diff --git a/internal/credential/rotate.go b/internal/credential/rotate.go index d2d35d4..7f1c518 100644 --- a/internal/credential/rotate.go +++ b/internal/credential/rotate.go @@ -161,14 +161,16 @@ func (c *Config) ValidateServiceAccountKey( ticker := time.NewTicker(200 * time.Millisecond) defer ticker.Stop() - timeout := time.After(c.validateServiceAccountKeyTimeout) + ctx, cancel := context.WithTimeout(ctx, c.validateServiceAccountKeyTimeout) + defer cancel() for { select { case <-ctx.Done(): + if ctx.Err() == context.DeadlineExceeded { + return fmt.Errorf("failed to validate service account key before timeout: %w", errors.Join(validatePermissionsErr, validationCallbackErr)) + } return ctx.Err() - case <-timeout: - return errors.Join(validatePermissionsErr, validationCallbackErr) case <-ticker.C: _, validatePermissionsErr = c.ValidateIamPermissions(ctx, permissions, opts...) diff --git a/internal/credential/rotate_test.go b/internal/credential/rotate_test.go index aa77bd3..277dd37 100644 --- a/internal/credential/rotate_test.go +++ b/internal/credential/rotate_test.go @@ -269,7 +269,7 @@ func TestValidateIamPermissions(t *testing.T) { func TestValidateServiceAccountKey(t *testing.T) { ctx := context.Background() testResourceServer := &testResourceServer{} - testValidateTimeout := 200 * time.Millisecond + testValidateTimeout := 1 * time.Second tests := []struct { name string @@ -329,7 +329,7 @@ func TestValidateServiceAccountKey(t *testing.T) { IAMServiceAccountKeysDeletePermission, }, testIamPermissionsError: errors.New("IAM permissions error"), - expectedError: errors.New("IAM permissions error"), + expectedError: errors.New("failed to validate IAM permissions"), }, }