Exemple #1
0
        private async Task <string> GetOrCreateExtensionKey(string extensionName)
        {
            var    hostSecrets = _secretManager.GetHostSecretsAsync().GetAwaiter().GetResult();
            string keyName     = GetKeyName(extensionName);
            string keyValue    = null;

            if (!hostSecrets.SystemKeys.TryGetValue(keyName, out keyValue))
            {
                // if the requested secret doesn't exist, create it on demand
                keyValue = SecretManager.GenerateSecret();
                await _secretManager.AddOrUpdateFunctionSecretAsync(keyName, keyValue, HostKeyScopes.SystemKeys, ScriptSecretsType.Host);
            }

            return(keyValue);
        }
        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());
            }
        }
Exemple #3
0
        private async Task <string> GetOrCreateExtensionKey(string extensionName)
        {
            ISecretManager secretManager = _secretManagerProvider.Current;
            var            hostSecrets   = await secretManager.GetHostSecretsAsync();

            string keyName = GetKeyName(extensionName);
            string keyValue;

            if (!hostSecrets.SystemKeys.TryGetValue(keyName, out keyValue))
            {
                // if the requested secret doesn't exist, create it on demand
                keyValue = SecretGenerator.GenerateSystemKeyValue();
                await secretManager.AddOrUpdateFunctionSecretAsync(keyName, keyValue, HostKeyScopes.SystemKeys, ScriptSecretsType.Host);
            }

            return(keyValue);
        }