diff --git a/pkg/yamll/dependency.go b/pkg/yamll/dependency.go index 9562404..b1b454b 100644 --- a/pkg/yamll/dependency.go +++ b/pkg/yamll/dependency.go @@ -32,10 +32,11 @@ type Dependency struct { // Auth holds the authentication information to resolve the remote yaml files. type Auth struct { - UserName string `json:"user_name,omitempty" yaml:"user_name,omitempty" mapstructure:"user_name"` - Password string `json:"password,omitempty" yaml:"password,omitempty" mapstructure:"password"` - BarerToken string `json:"barer_token,omitempty" yaml:"barer_token,omitempty" mapstructure:"barer_token"` - CaContent string `json:"ca_content,omitempty" yaml:"ca_content,omitempty" mapstructure:"ca_content"` + UserName string `json:"user_name,omitempty" yaml:"user_name,omitempty"` + Password string `json:"password,omitempty" yaml:"password,omitempty"` + BarerToken string `json:"barer_token,omitempty" yaml:"barer_token,omitempty"` + CaContent string `json:"ca_content,omitempty" yaml:"ca_content,omitempty"` + SSHKey string `json:"ssh_key,omitempty" yaml:"ssh_key,omitempty"` } // ResolveDependencies addresses the dependencies of YAML imports specified in the YAML files. @@ -88,6 +89,7 @@ func (cfg *Config) ResolveDependencies(routes map[string]*YamlData, dependencies return routes, nil } +// GetDependencyData reads the imports analyses it and generates Dependency data for it. func (cfg *Config) GetDependencyData(dependency string) (*Dependency, error) { imports := strings.Split(dependency, ";") runeSlice := []rune(imports[0]) @@ -114,51 +116,67 @@ func (cfg *Config) GetDependencyData(dependency string) (*Dependency, error) { return dependencyData, nil } +// ReadData actually reads the data from the identified import. func (dependency *Dependency) ReadData(log *slog.Logger) (string, error) { log.Debug("dependency file type identification", slog.String("type", dependency.Type)) switch { case dependency.Type == TypeURL: - httpClient := resty.New() - if len(dependency.Auth.BarerToken) != 0 { - httpClient.SetAuthToken(dependency.Auth.BarerToken) - } + return dependency.URL() + case dependency.Type == TypeGit: + return dependency.Git() + case dependency.Type == TypeFile: + return dependency.File() + default: + return "", &errors.YamllError{Message: fmt.Sprintf("reading data from of type '%s' is not supported", dependency.Type)} + } +} - if len(dependency.Auth.UserName) != 0 && len(dependency.Auth.Password) != 0 { - httpClient.SetBasicAuth(dependency.Auth.UserName, dependency.Auth.Password) - } +// Git reads the data from the Git import. +func (dependency *Dependency) Git() (string, error) { + return "", &errors.YamllError{Message: "does not support dependency type 'git' at the moment"} +} - if len(dependency.Auth.CaContent) != 0 { - certPool := x509.NewCertPool() - certPool.AppendCertsFromPEM([]byte(dependency.Auth.CaContent)) - httpClient.SetTLSClientConfig(&tls.Config{RootCAs: certPool}) //nolint:gosec - } else { - httpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) //nolint:gosec - } +// URL reads the data from the URL import. +func (dependency *Dependency) URL() (string, error) { + httpClient := resty.New() + if len(dependency.Auth.BarerToken) != 0 { + httpClient.SetAuthToken(dependency.Auth.BarerToken) + } - resp, err := httpClient.R().Get(dependency.Path) - if err != nil { - return "", err - } + if len(dependency.Auth.UserName) != 0 && len(dependency.Auth.Password) != 0 { + httpClient.SetBasicAuth(dependency.Auth.UserName, dependency.Auth.Password) + } - return resp.String(), err - case dependency.Type == TypeGit: - return "", &errors.YamllError{Message: "does not support dependency type 'git' at the moment"} - case dependency.Type == TypeFile: - absYamlFilePath, err := filepath.Abs(dependency.Path) - if err != nil { - return "", err - } + if len(dependency.Auth.CaContent) != 0 { + certPool := x509.NewCertPool() + certPool.AppendCertsFromPEM([]byte(dependency.Auth.CaContent)) + httpClient.SetTLSClientConfig(&tls.Config{RootCAs: certPool}) //nolint:gosec + } else { + httpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) //nolint:gosec + } - yamlFileData, err := os.ReadFile(absYamlFilePath) - if err != nil { - return "", &errors.YamllError{Message: fmt.Sprintf("reading YAML dependency errored with: '%v'", err)} - } + resp, err := httpClient.R().Get(dependency.Path) + if err != nil { + return "", err + } - return string(yamlFileData), nil - default: - return "", &errors.YamllError{Message: fmt.Sprintf("reading data from of type '%s' is not supported", dependency.Type)} + return resp.String(), err +} + +// File reads the data from the File import. +func (dependency *Dependency) File() (string, error) { + absYamlFilePath, err := filepath.Abs(dependency.Path) + if err != nil { + return "", err } + + yamlFileData, err := os.ReadFile(absYamlFilePath) + if err != nil { + return "", &errors.YamllError{Message: fmt.Sprintf("reading YAML dependency errored with: '%v'", err)} + } + + return string(yamlFileData), nil } func (dependency *Dependency) identifyType() { diff --git a/pkg/yamll/yamll.go b/pkg/yamll/yamll.go index 2b77a70..46775b4 100644 --- a/pkg/yamll/yamll.go +++ b/pkg/yamll/yamll.go @@ -9,10 +9,9 @@ import ( // YamlData holds information of yaml file and its dependency tree. type YamlData struct { - Root bool `json:"root,omitempty" yaml:"root,omitempty"` - Imported bool `json:"imported,omitempty" yaml:"imported,omitempty"` - File string `json:"file,omitempty" yaml:"file,omitempty"` - /*Dependency []string `json:"dependency,omitempty" yaml:"dependency,omitempty"`*/ + Root bool `json:"root,omitempty" yaml:"root,omitempty"` + Imported bool `json:"imported,omitempty" yaml:"imported,omitempty"` + File string `json:"file,omitempty" yaml:"file,omitempty"` Dependency []*Dependency `json:"dependency,omitempty" yaml:"dependency,omitempty"` DataRaw string `json:"data_raw,omitempty" yaml:"data_raw,omitempty"` Data map[string]interface{} `json:"data,omitempty" yaml:"data,omitempty"`