Exemplo n.º 1
0
        public async Task <IActionResult> Delete(string id)
        {
            if (_extensionBundleManager.IsExtensionBundleConfigured())
            {
                return(BadRequest(Resources.ExtensionBundleBadRequestDelete));
            }

            if (!_environment.IsPersistentFileSystemAvailable())
            {
                return(BadRequest(Resources.ErrorDeletingExtension));
            }

            // TODO: Check if we have an active job

            var job = await CreateJob(new ExtensionPackageReference()
            {
                Id = id, Version = string.Empty
            });

            await _extensionsManager.DeleteExtensions(id)
            .ContinueWith(t => ProcessJobTaskResult(t, job.Id));

            var    apiModel = ApiModelUtility.CreateApiModel(job, Request, $"jobs/{job.Id}");
            string action   = $"{Request.Scheme}://{Request.Host.ToUriComponent()}{Url.Action(nameof(GetJobs), "Extensions", new { id = job.Id })}{Request.QueryString}";

            return(Accepted(action, apiModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> InstallExtension(ExtensionPackageReference package, bool verifyConflict = true)
        {
            if (package == null)
            {
                return(BadRequest());
            }

            if (verifyConflict)
            {
                // If a different version of this extension is already installed, conflict:
                var extensions = await _extensionsManager.GetExtensions();

                if (extensions.Any(e => e.Id.Equals(package.Id) && !e.Version.Equals(package.Version)))
                {
                    return(StatusCode(StatusCodes.Status409Conflict));
                }
            }

            ExtensionsRestoreJob job = await CreateJob(package);

            string jobId   = job.Id;
            var    addTask = _extensionsManager.AddExtensions(package)
                             .ContinueWith(t => ProcessJobTaskResult(t, jobId));

            var    apiModel = ApiModelUtility.CreateApiModel(job, Request, $"jobs/{job.Id}");
            string action   = $"{Request.Scheme}://{Request.Host.ToUriComponent()}{Url.Action(nameof(GetJobs), "Extensions", new { id = job.Id })}{Request.QueryString}";

            return(Accepted(action, apiModel));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetJobs()
        {
            IEnumerable <ExtensionsRestoreJob> jobs = await GetInProgressJobs();

            var jobContent = new { jobs };
            var result     = ApiModelUtility.CreateApiModel(jobContent, Request);

            return(Ok(result));
        }
Exemplo n.º 4
0
        private IHttpActionResult GetKeysResult(IDictionary <string, string> keys)
        {
            var keysContent = new
            {
                keys = keys.Select(k => new { name = k.Key, value = k.Value })
            };

            var keyResponse = ApiModelUtility.CreateApiModel(keysContent, Request);

            return(Ok(keyResponse));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Get(string functionName, string name)
        {
            IDictionary <string, string> functionKeys = await GetFunctionKeys(functionName);

            if (functionKeys != null && functionKeys.TryGetValue(name, out string keyValue))
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = name, value = keyValue }, Request);
                return(Ok(keyResponse));
            }

            return(NotFound());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> GetJobs(string id)
        {
            ExtensionsRestoreJob job = await GetJob(id);

            if (job == null)
            {
                return(NotFound());
            }

            var apiModel = ApiModelUtility.CreateApiModel(job, Request);

            return(Ok(apiModel));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Get()
        {
            IEnumerable <ExtensionPackageReference> extensions = await _extensionsManager.GetExtensions();

            var extensionsContent = new
            {
                extensions = extensions
            };

            var result = ApiModelUtility.CreateApiModel(extensionsContent, Request);

            return(Ok(result));
        }
Exemplo n.º 8
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));
            }
        }
        public async Task <IActionResult> Delete(string id)
        {
            // TODO: Check if we have an active job

            var job = await CreateJob();

            await _extensionsManager.DeleteExtensions(id)
            .ContinueWith(t => ProcessJobTaskResult(t, job.Id));

            var    apiModel = ApiModelUtility.CreateApiModel(job, Request, $"jobs/{job.Id}");
            string action   = $"{Request.Scheme}://{Request.Host.ToUriComponent()}{Url.Action(nameof(GetJobs), "Extensions", new { id = job.Id })}{Request.QueryString}";

            return(Accepted(action, apiModel));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> GetHostKey(string name)
        {
            string hostKeyScope = GetHostKeyScopeForRequest();

            Dictionary <string, string> keys = await GetHostSecretsByScope(hostKeyScope, true);

            string keyValue = null;

            if (keys?.TryGetValue(name, out keyValue) ?? false)
            {
                var keyResponse = ApiModelUtility.CreateApiModel(new { name = name, value = keyValue }, Request);
                return(Ok(keyResponse));
            }

            return(NotFound());
        }
        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());
            }
        }
Exemplo n.º 13
0
        public async Task <IActionResult> InstallExtension(ExtensionPackageReferenceWithActions package, bool verifyConflict = true)
        {
            if (package == null)
            {
                return(BadRequest());
            }

            if (_extensionBundleManager.IsExtensionBundleConfigured())
            {
                return(BadRequest(Resources.ExtensionBundleBadRequestInstall));
            }

            if (!_environment.IsPersistentFileSystemAvailable())
            {
                return(BadRequest(Resources.ErrorInstallingExtension));
            }

            if (verifyConflict)
            {
                // If a different version of this extension is already installed, conflict:
                var extensions = await _extensionsManager.GetExtensions();

                if (extensions.Any(e => e.Id.Equals(package.Id) && !e.Version.Equals(package.Version)))
                {
                    return(StatusCode(StatusCodes.Status409Conflict));
                }
            }

            ExtensionsRestoreJob job = await CreateJob(package);

            string jobId = job.Id;

            Enum.TryParse(package.PostInstallActions, true, out ExtensionPostInstallActions postInstallActions);
            var addTask = _extensionsManager.AddExtensions(package)
                          .ContinueWith(t => ProcessJobTaskResult(t, jobId, postInstallActions));

            var    apiModel = ApiModelUtility.CreateApiModel(job, Request, $"jobs/{job.Id}");
            string action   = $"{Request.Scheme}://{Request.Host.ToUriComponent()}{Url.Action(nameof(GetJobs), "Extensions", new { id = job.Id })}{Request.QueryString}";

            return(Accepted(action, apiModel));
        }