Skip to content

Commit

Permalink
Fix URL dependency type identification incase of http based URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jun 18, 2024
1 parent c130cab commit 210eeb3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/fixtures/import.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##++internal/fixtures/base.yaml
##++git+https://github.com/nikhilsbhat/yamll@main?path=internal/fixtures/base2.yaml;{"user_name":"${GIT_USERNAME}","password":"${GITHUB_TOKEN}"}
##++https://run.mocky.io/v3/e85820ed-2f5d-4633-9509-71fe7f4c007f
##++http://localhost:3000/database.yaml
#++path/to/test.yaml
#++https://test.com/test.yaml;{"user_name":"username","password":"pass","ca_content":"ca_content"}
#++https://test.com/test.yaml;{"user_name":"${username}","password":"${pass}","ca_content":"${ca_content}"}
Expand Down
21 changes: 15 additions & 6 deletions pkg/yamll/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

const (
TypeURL = "https"
TypeURL = "http"
TypeGit = "git+"
TypeFile = "file"
defaultDirPermissions = 0o755
Expand Down Expand Up @@ -137,15 +137,15 @@ func (cfg *Config) GetDependencyData(dependency string) (*Dependency, error) {

// 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))
log.Debug("dependency file type identified", slog.String("type", dependency.Type))

switch {
case dependency.Type == TypeURL:
return dependency.URL()
return dependency.URL(log)
case dependency.Type == TypeGit:
return dependency.Git(log)
case dependency.Type == TypeFile:
return dependency.File()
return dependency.File(log)
default:
return "", &errors.YamllError{Message: fmt.Sprintf("reading data from of type '%s' is not supported", dependency.Type)}
}
Expand Down Expand Up @@ -229,21 +229,30 @@ func (dependency *Dependency) Git(log *slog.Logger) (string, error) {
}

// URL reads the data from the URL import.
func (dependency *Dependency) URL() (string, error) {
func (dependency *Dependency) URL(log *slog.Logger) (string, error) {
httpClient := resty.New()

if len(dependency.Auth.BarerToken) != 0 {
log.Debug("using token based auth for remote URL", slog.Any("url", dependency.Path))

httpClient.SetAuthToken(dependency.Auth.BarerToken)
}

if len(dependency.Auth.UserName) != 0 && len(dependency.Auth.Password) != 0 {
log.Debug("using basic auth for remote URL", slog.Any("url", dependency.Path))

httpClient.SetBasicAuth(dependency.Auth.UserName, dependency.Auth.Password)
}

if len(dependency.Auth.CaContent) != 0 {
log.Debug("using CA for authentication for remote URL", slog.Any("url", dependency.Path))

certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM([]byte(dependency.Auth.CaContent))
httpClient.SetTLSClientConfig(&tls.Config{RootCAs: certPool}) //nolint:gosec
} else {
log.Debug("skipping TLS verification")

httpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) //nolint:gosec
}

Expand All @@ -256,7 +265,7 @@ func (dependency *Dependency) URL() (string, error) {
}

// File reads the data from the File import.
func (dependency *Dependency) File() (string, error) {
func (dependency *Dependency) File(_ *slog.Logger) (string, error) {
absYamlFilePath, err := filepath.Abs(dependency.Path)
if err != nil {
return "", err
Expand Down

0 comments on commit 210eeb3

Please sign in to comment.