Match remote sync credentials by host

This commit is contained in:
Joe Julian
2026-04-06 22:22:18 -07:00
parent cb6fbd05a3
commit 43ef58936b
2 changed files with 80 additions and 2 deletions
+31 -2
View File
@@ -10,6 +10,7 @@ import (
"image"
"image/color"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
@@ -2319,6 +2320,34 @@ func normalizeRemoteCredentialURL(raw string) string {
return raw
}
func remoteCredentialURLMatches(candidate, target string) bool {
candidate = normalizeRemoteCredentialURL(candidate)
target = normalizeRemoteCredentialURL(target)
if candidate == "" || target == "" {
return false
}
if candidate == target {
return true
}
candidateURL, err := url.Parse(candidate)
if err != nil {
return false
}
targetURL, err := url.Parse(target)
if err != nil {
return false
}
if !strings.EqualFold(candidateURL.Hostname(), targetURL.Hostname()) {
return false
}
candidatePath := strings.TrimRight(candidateURL.EscapedPath(), "/")
targetPath := strings.TrimRight(targetURL.EscapedPath(), "/")
if candidatePath == "" || candidatePath == "/" || targetPath == "" || targetPath == "/" {
return true
}
return strings.HasPrefix(targetPath, candidatePath) || strings.HasPrefix(candidatePath, targetPath)
}
func (u *ui) matchingAdvancedSyncRemoteCredentialEntries() []vault.Entry {
if sanitizeSyncSourceMode(u.syncSourceMode) != syncSourceRemote {
return nil
@@ -2346,7 +2375,7 @@ func (u *ui) matchingAdvancedSyncRemoteCredentialEntries() []vault.Entry {
matches = append(matches, entry)
}
for _, entry := range entries {
if normalizeRemoteCredentialURL(entry.URL) != baseURL {
if !remoteCredentialURLMatches(entry.URL, baseURL) {
continue
}
appendMatch(entry)
@@ -2364,7 +2393,7 @@ func (u *ui) matchingAdvancedSyncRemoteCredentialEntries() []vault.Entry {
if !ok {
continue
}
if normalizeRemoteCredentialURL(profile.BaseURL) != baseURL {
if !remoteCredentialURLMatches(profile.BaseURL, baseURL) {
continue
}
if remotePath != "" && strings.TrimSpace(profile.Path) != remotePath && strings.TrimSpace(record.Path) != remotePath {