Example #1
0
        private async Task <IActionResult> AddOrUpdateSecretAsync(string keyName, string value, string keyScope, ScriptSecretsType secretsType)
        {
            if (secretsType == ScriptSecretsType.Function && keyScope != null && !IsFunction(keyScope))
            {
                return(NotFound());
            }

            KeyOperationResult operationResult;

            if (secretsType == ScriptSecretsType.Host && string.Equals(keyName, MasterKeyName, StringComparison.OrdinalIgnoreCase))
            {
                operationResult = await _secretManagerProvider.Current.SetMasterKeyAsync(value);
            }
            else
            {
                operationResult = await _secretManagerProvider.Current.AddOrUpdateFunctionSecretAsync(keyName, value, keyScope, secretsType);
            }

            _logger.LogDebug(string.Format(Resources.TraceKeysApiSecretChange, keyName, keyScope ?? "host", operationResult.Result));

            switch (operationResult.Result)
            {
            case OperationResult.Created:
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = keyName, value = operationResult.Secret }, Request);
                await _functionsSyncManager.TrySyncTriggersAsync();

                return(Created(ApiModelUtility.GetBaseUri(Request), keyResponse));
            }

            case OperationResult.Updated:
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = keyName, value = operationResult.Secret }, Request);
                await _functionsSyncManager.TrySyncTriggersAsync();

                return(Ok(keyResponse));
            }

            case OperationResult.NotFound:
                return(NotFound());

            case OperationResult.Conflict:
                return(StatusCode(StatusCodes.Status409Conflict));

            case OperationResult.Forbidden:
                return(StatusCode(StatusCodes.Status403Forbidden));

            default:
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        private async Task <IHttpActionResult> AddOrUpdateSecretAsync(string keyName, string value, string keyScope, ScriptSecretsType secretsType)
        {
            if (secretsType == ScriptSecretsType.Function && keyScope != null && !_scriptHostManager.Instance.IsFunction(keyScope))
            {
                return(NotFound());
            }

            KeyOperationResult operationResult;

            if (secretsType == ScriptSecretsType.Host && string.Equals(keyName, MasterKeyName, StringComparison.OrdinalIgnoreCase))
            {
                operationResult = await _secretManager.SetMasterKeyAsync(value);
            }
            else
            {
                operationResult = await _secretManager.AddOrUpdateFunctionSecretAsync(keyName, value, keyScope, secretsType);
            }

            string message = string.Format(Resources.TraceKeysApiSecretChange, keyName, keyScope ?? "host", operationResult.Result);

            _traceWriter.Verbose(message);
            _logger?.LogDebug(message);

            switch (operationResult.Result)
            {
            case OperationResult.Created:
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = keyName, value = operationResult.Secret }, Request);
                return(Created(ApiModelUtility.GetBaseUri(Request), keyResponse));
            }

            case OperationResult.Updated:
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = keyName, value = operationResult.Secret }, Request);
                return(Ok(keyResponse));
            }

            case OperationResult.NotFound:
                return(NotFound());

            case OperationResult.Conflict:
                return(Conflict());

            default:
                return(InternalServerError());
            }
        }
        private IHttpActionResult AddOrUpdateFunctionSecret(string keyName, string value, string functionName = null)
        {
            if (functionName != null &&
                !_scriptHostManager.Instance.Functions.Any(f => string.Equals(f.Name, functionName, StringComparison.OrdinalIgnoreCase)))
            {
                return(NotFound());
            }

            KeyOperationResult operationResult;

            if (functionName == null && string.Equals(keyName, MasterKeyName, StringComparison.OrdinalIgnoreCase))
            {
                operationResult = _secretManager.SetMasterKey(value);
            }
            else
            {
                operationResult = _secretManager.AddOrUpdateFunctionSecret(keyName, value, functionName);
            }

            _traceWriter.VerboseFormat(Resources.TraceKeysApiSecretChange, keyName, functionName ?? "host", operationResult.Result);

            switch (operationResult.Result)
            {
            case OperationResult.Created:
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = keyName, value = operationResult.Secret }, Request);
                return(Created(ApiModelUtility.GetBaseUri(Request), keyResponse));
            }

            case OperationResult.Updated:
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = keyName, value = operationResult.Secret }, Request);
                return(Ok(keyResponse));
            }

            case OperationResult.NotFound:
                return(NotFound());

            case OperationResult.Conflict:
                return(Conflict());

            default:
                return(InternalServerError());
            }
        }