Skip to content

Commit 4a2a8fe

Browse files
committed
OCPBUGS-29900:fix the Metric cco_credentials_mode issue
1 parent 8f1a631 commit 4a2a8fe

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

pkg/operator/metrics/metrics.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package metrics
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67
"time"
78

@@ -252,11 +253,17 @@ func (a *credRequestAccumulator) processCR(cr *credreqv1.CredentialsRequest, cco
252253

253254
isPodIdentity, err := credRequestIsPodIdentity(cr, cloudType, a.kubeClient)
254255
if err != nil {
255-
a.logger.WithError(err).Error("failed to determine whether CredentialsRequest is of type STS")
256+
a.logger.WithError(err).WithField("credentialsRequest", cr.Name).Error("failed to determine whether CredentialsRequest is of type pod identity")
256257
}
257258

258259
if isPodIdentity {
259260
a.podIdentityCredentials++
261+
a.logger.WithFields(log.Fields{
262+
"credentialsRequest": cr.Name,
263+
"namespace": cr.Namespace,
264+
"cloudType": cloudType,
265+
"secretRef": fmt.Sprintf("%s/%s", cr.Spec.SecretRef.Namespace, cr.Spec.SecretRef.Name),
266+
}).Debug("detected pod identity credentials")
260267
}
261268

262269
// Skip reporting conditions if CCO is disabled, as we shouldn't be alerting in that case, except for stale credentials.
@@ -364,21 +371,51 @@ func (a *credRequestAccumulator) setMetrics() {
364371
}
365372

366373
func credRequestIsPodIdentity(cr *credreqv1.CredentialsRequest, cloudType string, kubeClient client.Client) (bool, error) {
374+
// Check if SecretRef is set
375+
if cr.Spec.SecretRef.Name == "" || cr.Spec.SecretRef.Namespace == "" {
376+
log.WithFields(log.Fields{
377+
"credentialsRequest": cr.Name,
378+
}).Debug("keys Name or Namespace is null")
379+
return false, nil
380+
}
381+
367382
secretKey := types.NamespacedName{Name: cr.Spec.SecretRef.Name, Namespace: cr.Spec.SecretRef.Namespace}
368383
secret := &corev1.Secret{}
369384

370385
err := kubeClient.Get(context.TODO(), secretKey, secret)
371386
if errors.IsNotFound(err) {
372387
// Secret for CredReq doesn't exist so we can't query it
388+
log.WithFields(log.Fields{
389+
"credentialsRequest": cr.Name,
390+
"secretName": secret.Name,
391+
"secretNamespace": secret.Namespace,
392+
}).Debug("AWS secret not found")
373393
return false, nil
374394
} else if err != nil {
395+
log.WithFields(log.Fields{
396+
"credentialsRequest": cr.Name,
397+
"secretName": secret.Name,
398+
"secretNamespace": secret.Namespace,
399+
}).Debug("AWS secret not loaded due to other error")
375400
return false, err
376401
}
377402

378403
switch cloudType {
379404
case "AWSProviderSpec":
380405
secretData, ok := secret.Data[constants.AWSSecretDataCredentialsKey]
381406
if !ok {
407+
// Log available keys for debugging
408+
availableKeys := make([]string, 0, len(secret.Data))
409+
for k := range secret.Data {
410+
availableKeys = append(availableKeys, k)
411+
}
412+
log.WithFields(log.Fields{
413+
"credentialsRequest": cr.Name,
414+
"secretName": secret.Name,
415+
"secretNamespace": secret.Namespace,
416+
"expectedKey": constants.AWSSecretDataCredentialsKey,
417+
"availableKeys": availableKeys,
418+
}).Debug("AWS secret missing expected credentials key")
382419
return false, nil
383420
}
384421

@@ -388,10 +425,35 @@ func credRequestIsPodIdentity(cr *credreqv1.CredentialsRequest, cloudType string
388425
return true, nil
389426
}
390427

428+
// Log for debugging when AWS secret doesn't contain STS indicators
429+
preview := string(secretData)
430+
if len(preview) > 100 {
431+
preview = preview[:100] + "..."
432+
}
433+
log.WithFields(log.Fields{
434+
"credentialsRequest": cr.Name,
435+
"secretName": secret.Name,
436+
"secretNamespace": secret.Namespace,
437+
"secretDataPreview": preview,
438+
}).Debug("AWS secret does not contain web_identity_token_file, not STS credentials")
439+
391440
return false, nil
392441
case "AzureProviderSpec":
393442
_, ok := secret.Data[azure.AzureFederatedTokenFile]
394443
return ok, nil
444+
case "GCPProviderSpec":
445+
secretData, ok := secret.Data["service_account.json"]
446+
if !ok {
447+
return false, nil
448+
}
449+
450+
// external_account type is a clear indicator that the credentials
451+
// are configured for GCP Workload Identity Federation (WIF)
452+
if strings.Contains(string(secretData), "external_account") {
453+
return true, nil
454+
}
455+
456+
return false, nil
395457
default:
396458
return false, nil
397459
}

pkg/operator/metrics/metrics_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,26 @@ func TestCredentialsRequests(t *testing.T) {
256256
assert.Equal(t, 1, accumulator.podIdentityCredentials)
257257
},
258258
},
259+
{
260+
name: "cco manual mode with GCP Workload Identity Federation",
261+
ccoDisabled: true,
262+
existingObjects: []runtime.Object{
263+
testSecret("wif-namespace", "wif-name", map[string][]byte{
264+
"service_account.json": []byte(`{"type": "external_account", "audience": "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider"}`),
265+
}),
266+
testSecret("non-wif-namespace", "non-wif-name", map[string][]byte{
267+
"service_account.json": []byte(`{"type": "service_account", "project_id": "my-project"}`),
268+
}),
269+
},
270+
credReqs: []credreqv1.CredentialsRequest{
271+
testCredRequestWithSecretRef(testGCPCredRequest("wif-style"), "wif-namespace", "wif-name"),
272+
testCredRequestWithSecretRef(testGCPCredRequest("non-wif-style"), "non-wif-namespace", "non-wif-name"),
273+
},
274+
validate: func(t *testing.T, accumulator *credRequestAccumulator) {
275+
assert.Equal(t, 2, accumulator.crTotals["gcp"])
276+
assert.Equal(t, 1, accumulator.podIdentityCredentials)
277+
},
278+
},
259279
}
260280

261281
for _, test := range tests {

0 commit comments

Comments
 (0)