ParseStorageFlag parses the --vsa-upload flag format Supported formats: - rekor@https://rekor.sigstore.dev - local@/path/to/directory - rekor?server=custom.rekor.com&timeout=30s
(storageFlag string)
| 52 | // - local@/path/to/directory |
| 53 | // - rekor?server=custom.rekor.com&timeout=30s |
| 54 | func ParseStorageFlag(storageFlag string) (*StorageConfig, error) { |
| 55 | if storageFlag == "" { |
| 56 | return nil, fmt.Errorf("storage flag cannot be empty") |
| 57 | } |
| 58 | |
| 59 | config := &StorageConfig{ |
| 60 | Parameters: make(map[string]string), |
| 61 | } |
| 62 | |
| 63 | // Split on @ to separate backend from URL/config |
| 64 | var configPart string |
| 65 | if strings.Contains(storageFlag, "@") { |
| 66 | parts := strings.SplitN(storageFlag, "@", 2) |
| 67 | config.Backend = parts[0] |
| 68 | configPart = parts[1] |
| 69 | } else { |
| 70 | // No @ means it's just backend name, possibly with query params |
| 71 | if strings.Contains(storageFlag, "?") { |
| 72 | parts := strings.SplitN(storageFlag, "?", 2) |
| 73 | config.Backend = parts[0] |
| 74 | configPart = "?" + parts[1] // Add ? back for URL parsing |
| 75 | } else { |
| 76 | config.Backend = storageFlag |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Validate that backend is not empty |
| 81 | if config.Backend == "" { |
| 82 | return nil, fmt.Errorf("backend name cannot be empty") |
| 83 | } |
| 84 | |
| 85 | // Validate that backend is supported |
| 86 | supportedBackends := []string{"rekor", "local"} |
| 87 | isSupported := false |
| 88 | for _, supported := range supportedBackends { |
| 89 | if strings.ToLower(config.Backend) == supported { |
| 90 | isSupported = true |
| 91 | break |
| 92 | } |
| 93 | } |
| 94 | if !isSupported { |
| 95 | return nil, fmt.Errorf("unsupported backend '%s'. Supported backends: %s", config.Backend, strings.Join(supportedBackends, ", ")) |
| 96 | } |
| 97 | |
| 98 | // Parse URL-style parameters if present |
| 99 | if configPart != "" { |
| 100 | // If it doesn't start with http(s), add a dummy scheme for parsing |
| 101 | parseURL := configPart |
| 102 | if !strings.HasPrefix(configPart, "http") && !strings.HasPrefix(configPart, "?") { |
| 103 | parseURL = "dummy://" + configPart |
| 104 | } else if strings.HasPrefix(configPart, "?") { |
| 105 | parseURL = "dummy://dummy" + configPart |
| 106 | } |
| 107 | |
| 108 | parsed, err := url.Parse(parseURL) |
| 109 | if err != nil { |
| 110 | return nil, fmt.Errorf("invalid storage configuration format: %w", err) |
| 111 | } |