Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: secret referencing bug
  • Loading branch information
nimish-ks committed Oct 29, 2024
commit 5ce82c5d51c0a6d1ada5297550b28e206b68c73d
42 changes: 33 additions & 9 deletions phase/misc/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,54 @@ func PhaseGetContext(userData AppKeyResponse, opts GetContextOptions) (string, s
return "", "", "", fmt.Errorf("matching context not found")
}

// FindEnvironmentKey searches for an environment key with case-insensitive and partial matching.
// FindEnvironmentKey searches for an environment key with case-insensitive matching.
func FindEnvironmentKey(userData AppKeyResponse, opts FindEnvironmentKeyOptions) (*EnvironmentKey, error) {
lcEnvName := strings.ToLower(opts.EnvName)
lcAppName := strings.ToLower(opts.AppName)
lcEnvName := strings.ToLower(strings.TrimSpace(opts.EnvName))

for _, app := range userData.Apps {
if (opts.AppID != "" && app.ID == opts.AppID) ||
(opts.AppName != "" && (opts.AppName == "" || strings.Contains(strings.ToLower(app.Name), lcAppName))) {
// If no app specified, try all apps
if opts.AppName == "" && opts.AppID == "" {
for _, app := range userData.Apps {
for _, envKey := range app.EnvironmentKeys {
if strings.Contains(strings.ToLower(envKey.Environment.Name), lcEnvName) {
if strings.EqualFold(strings.TrimSpace(envKey.Environment.Name), lcEnvName) {
return &envKey, nil
}
}
}
} else {
// Check specific app
for _, app := range userData.Apps {
if (opts.AppID != "" && app.ID == opts.AppID) ||
(opts.AppName != "" && strings.EqualFold(app.Name, opts.AppName)) {

for _, envKey := range app.EnvironmentKeys {
if strings.EqualFold(strings.TrimSpace(envKey.Environment.Name), lcEnvName) {
return &envKey, nil
}
}
}
}
}
return nil, fmt.Errorf("environment key not found for app '%s' (ID: %s) and environment '%s'", opts.AppName, opts.AppID, opts.EnvName)

// If exact match not found, try partial matches
for _, app := range userData.Apps {
for _, envKey := range app.EnvironmentKeys {
envName := strings.ToLower(strings.TrimSpace(envKey.Environment.Name))
if strings.Contains(envName, lcEnvName) {
return &envKey, nil
}
}
}

return nil, fmt.Errorf("environment key not found for app '%s' (ID: %s) and environment '%s'",
opts.AppName, opts.AppID, opts.EnvName)
}

// normalizeTag replaces underscores with spaces and converts the string to lower case.
func normalizeTag(tag string) string {
return strings.ToLower(strings.Replace(tag, "_", " ", -1))
}

// tagMatches checks if the user-provided tag partially matches any of the secret tags.
// TagMatches checks if the user-provided tag partially matches any of the secret tags.
func TagMatches(secretTags []string, userTag string) bool {
normalizedUserTag := normalizeTag(userTag)
for _, tag := range secretTags {
Expand Down