/// <summary> /// Retreives the Repository Credentials from Header /// </summary> /// <returns>string header value</returns> private RepositoryCredentials GetRepsitoryCredentials() { IEnumerable <string> header = null; if (!this.Request.Headers.TryGetValues(Constants.RepositoryCredentialHeaderName, out header)) { return(null); } var credentials = header.FirstOrDefault(); RepositoryCredentials repositoryCredentials = new RepositoryCredentials(); //Dictionary<string, string> attributes = JsonConvert.DeserializeObject<Dictionary<string, string>>(credentials); repositoryCredentials.Attributes = new JavaScriptSerializer().Deserialize <Dictionary <string, string> >(credentials); // attributes = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(credentials); //repositoryCredentials = new RepositoryCredentials(); //JArray myObjects = JsonConvert.DeserializeObject<JArray>(credentials); //foreach (string v in myObjects) //{ // JToken token = JObject.Parse(v); // repositoryCredentials[token.SelectToken("key").Value<string>()] = token.SelectToken("value").Value<string>(); //} return(repositoryCredentials); }
public ContainerCreateParameters(string image, string tag, string startOptions, RepositoryCredentials imageSourceCreadentials, string command) { Image = image; Tag = tag; StartOptions = startOptions; ImageSourceCredentials = imageSourceCreadentials; Command = command; }
public Task <string> RepositoryLogoutAsync(RepositoryCredentials auth, CancellationToken ct) => RepositoryLogoutAsync(auth.RepositoryServer, ct);
public Task <string> RepositoryLoginAsync(RepositoryCredentials auth, CancellationToken ct) => RepositoryLoginAsync(auth.Username, auth.Password, auth.RepositoryServer, ct);
/// <summary> /// Downloads the file from Repository. /// </summary> /// <param name="fileId">File Id.</param> /// <param name="user">User instance.</param> /// <returns>File stream.</returns> private HttpResponseMessage DownloadFileFromRepository(int fileId, User user) { HttpError error; try { if (fileId <= 0) { error = new HttpError(string.Format(MessageStrings.Argument_Error_Message_Template, "fileId")); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, error)); } var file = this.fileService.GetFiles(p => p.FileId == fileId && p.CreatedBy == user.UserId).FirstOrDefault(); if (file == null) { error = new HttpError(MessageStrings.FileDoesntExist) { { "FileId", fileId } }; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, error)); } if (file.RepositoryId == null) { error = new HttpError(MessageStrings.File_Repository_Is_Null) { { "FileId", fileId } }; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, error)); } DM.Repository repository = this.repositoryService.GetRepositoryById((int)file.RepositoryId); if (repository == null) { error = new HttpError(MessageStrings.Repository_Not_Found) { { "RepositoryId", (int)file.RepositoryId } }; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, error)); } RepositoryCredentials repositoryCredentials = GetRepsitoryCredentials(); this.fileService = this.fileServiceFactory.GetFileService(repository.BaseRepository.Name); DataFile dataFile = this.fileService.DownLoadFileFromRepository(file, repository, user, repositoryCredentials); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(new MemoryStream(dataFile.FileContent)); result.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.APPLICATION_X_ZIP); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = dataFile.FileName; return(result); } catch (ArgumentNullException ane) { message = string.Format(MessageStrings.Argument_Error_Message_Template, ane.ParamName); status = HttpStatusCode.BadRequest; } catch (FileDownloadException downloadException) { if (downloadException.FileDownloadExceptionType == FileDownloadExceptionType.DownloadUrlNotFound.ToString()) { error = downloadException.GetHttpError(MessageStrings.Download_URL_Empty); } else { error = downloadException.GetHttpError(string.Empty); } return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error)); } catch (WebException ex) { // If status code is 404 then send the custom message indicating file does not exist in repository. // else read the message and send it to client as text. HttpResponseMessage response; if (ex.Status == WebExceptionStatus.ProtocolError && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound) { error = new HttpError(MessageStrings.FileDoesNotExistInRepository); response = Request.CreateErrorResponse(HttpStatusCode.NotFound, error); } else { string errorText = string.Empty; using (Stream st = ((System.Net.WebException)(ex)).Response.GetResponseStream()) using (StreamReader reader = new StreamReader(st)) { errorText = reader.ReadToEnd(); } error = new HttpError(errorText); response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error); } return(response); } return(Request.CreateErrorResponse(status, message)); }
/// <summary> /// Reads the YAML configuration and validates it it. /// </summary> public void LoadConfiguration() { try { string configPath = ApplicationConfiguration.ConfigPath ?? string.Empty; Logger.LogInformation($"Using configuration file '{configPath}'"); if (!File.Exists(configPath)) { throw new YamlConfigurationValidationException("Configuration file doesn't exists"); } var file = File.OpenText(configPath); var yamlConfiguration = Deserializer.Deserialize <YamlConfiguration?>(file); if (yamlConfiguration?.Repositories is null) { throw new YamlConfigurationValidationException("No repositories found in configuration"); } YamlConfiguration = yamlConfiguration; Repositories = yamlConfiguration.Repositories .Select(entry => { if (entry.Value.Url is null) { throw new YamlConfigurationValidationException($"Repository '{entry.Key}' without uri"); } RepositoryCredentials?credentials = null; if (entry.Value.Username != null || entry.Value.Password != null) { if (entry.Value.Username == null) { throw new YamlConfigurationValidationException($"Repository '{entry.Key}' with username but without password"); } if (entry.Value.Password == null) { throw new YamlConfigurationValidationException($"Repository '{entry.Key}' with password but without username"); } credentials = new RepositoryCredentials(entry.Value.Username, entry.Value.Password); } return(new RepositoryDescriptor(entry.Key, entry.Value.Url, credentials, entry.Value.Config)); }) .ToList(); } catch (FileNotFoundException exc) { throw new YamlConfigurationValidationException("Configuration file not found", exc); } catch (Exception exc) { throw new YamlConfigurationValidationException("Error loading configuration", exc); } }