|
| 1 | +package conjur |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/cyberark/conjur-api-go/conjurapi" |
| 9 | + "github.com/cyberark/conjur-authn-k8s-client/pkg/log" |
| 10 | + |
| 11 | + "github.com/cyberark/secrets-provider-for-k8s/pkg/log/messages" |
| 12 | +) |
| 13 | + |
| 14 | +// conjurClientWrapper wraps the conjur-api-go Client to provide |
| 15 | +// V2 batch retrieval with fallback to V1 for backwards compatibility |
| 16 | +type conjurClientWrapper struct { |
| 17 | + client *conjurapi.Client |
| 18 | + useV2 bool |
| 19 | +} |
| 20 | + |
| 21 | +// RetrieveBatchSecretsSafe attempts to use V2 batch retrieval API first, |
| 22 | +// falling back to V1 if V2 is not available |
| 23 | +func (w *conjurClientWrapper) RetrieveBatchSecretsSafe(variableIDs []string) (map[string][]byte, error) { |
| 24 | + if w.useV2 { |
| 25 | + secrets, err := w.retrieveBatchSecretsV2(variableIDs) |
| 26 | + if err != nil { |
| 27 | + if isV2NotAvailableError(err) { |
| 28 | + log.Warn(messages.CSPFK090E, err.Error()) |
| 29 | + w.useV2 = false |
| 30 | + } else { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + } else { |
| 34 | + return secrets, nil |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + log.Debug(messages.CSPFK017D, "Using V1 batch retrieval") |
| 39 | + return w.client.RetrieveBatchSecretsSafe(variableIDs) |
| 40 | +} |
| 41 | + |
| 42 | +// retrieveBatchSecretsV2 uses the V2 batch retrieval API |
| 43 | +func (w *conjurClientWrapper) retrieveBatchSecretsV2(variableIDs []string) (map[string][]byte, error) { |
| 44 | + v2Client := w.client.V2() |
| 45 | + if v2Client == nil { |
| 46 | + return nil, fmt.Errorf("V2 client not available") |
| 47 | + } |
| 48 | + |
| 49 | + // V2 API expects variable paths without the account:variable: prefix |
| 50 | + // Normalize IDs: "account:variable:path/to/secret" -> "path/to/secret" |
| 51 | + normalizedIDs := make([]string, len(variableIDs)) |
| 52 | + originalIDMap := make(map[string]string) // maps normalized ID -> original ID |
| 53 | + |
| 54 | + for i, id := range variableIDs { |
| 55 | + normalizedID := normalizeVariableIdForV2(id) |
| 56 | + normalizedIDs[i] = normalizedID |
| 57 | + originalIDMap[normalizedID] = id |
| 58 | + } |
| 59 | + |
| 60 | + batchResp, err := v2Client.BatchRetrieveSecrets(normalizedIDs) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + secrets := make(map[string][]byte) |
| 66 | + var failedSecrets []string |
| 67 | + |
| 68 | + for _, secret := range batchResp.Secrets { |
| 69 | + if secret.Status == http.StatusOK { |
| 70 | + // Success - map back to original ID format |
| 71 | + originalID := originalIDMap[secret.ID] |
| 72 | + if originalID == "" { |
| 73 | + originalID = secret.ID |
| 74 | + } |
| 75 | + secrets[originalID] = []byte(secret.Value) |
| 76 | + } else { |
| 77 | + // Failed - track for error reporting (use original ID if available) |
| 78 | + originalID := originalIDMap[secret.ID] |
| 79 | + if originalID == "" { |
| 80 | + originalID = secret.ID |
| 81 | + } |
| 82 | + failedSecrets = append(failedSecrets, fmt.Sprintf("%s (status: %d)", originalID, secret.Status)) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if len(failedSecrets) > 0 { |
| 87 | + log.Warn(messages.CSPFK091E, strings.Join(failedSecrets, ", ")) |
| 88 | + } |
| 89 | + |
| 90 | + if len(secrets) == 0 { |
| 91 | + return nil, fmt.Errorf(messages.CSPFK092E) |
| 92 | + } |
| 93 | + |
| 94 | + log.Info(messages.CSPFK036I, len(secrets)) |
| 95 | + return secrets, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (w *conjurClientWrapper) Resources(filter *conjurapi.ResourceFilter) ([]map[string]interface{}, error) { |
| 99 | + return w.client.Resources(filter) |
| 100 | +} |
| 101 | + |
| 102 | +func (w *conjurClientWrapper) Cleanup() { |
| 103 | + w.client.Cleanup() |
| 104 | +} |
| 105 | + |
| 106 | +// isV2NotAvailableError checks if the error indicates V2 API is not available |
| 107 | +func isV2NotAvailableError(err error) bool { |
| 108 | + if err == nil { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + errMsg := err.Error() |
| 113 | + indicators := []string{ |
| 114 | + "not supported in", |
| 115 | + "404", |
| 116 | + } |
| 117 | + |
| 118 | + for _, indicator := range indicators { |
| 119 | + if strings.Contains(errMsg, indicator) { |
| 120 | + return true |
| 121 | + } |
| 122 | + } |
| 123 | + return false |
| 124 | +} |
| 125 | + |
| 126 | +// normalizeVariableIdForV2 converts a variable ID to the format expected by V2 API |
| 127 | +// The V2 API expects just the variable path (e.g., "secrets/test_secret") |
| 128 | +// not the full identifier format (e.g., "my-account:variable:secrets/test_secret") |
| 129 | +func normalizeVariableIdForV2(fullVariableId string) string { |
| 130 | + variableIdParts := strings.SplitN(fullVariableId, ":", 3) |
| 131 | + if len(variableIdParts) == 3 { |
| 132 | + // Format is "account:variable:path" -> return just "path" |
| 133 | + return variableIdParts[2] |
| 134 | + } |
| 135 | + // Already in correct format or unknown format, return as-is |
| 136 | + return fullVariableId |
| 137 | +} |
0 commit comments