Skip to content

Commit

Permalink
feat(events): Retrieve OIDC token in gcpPublishEvent (#4917)
Browse files Browse the repository at this point in the history
Co-authored-by: jliempt <>
  • Loading branch information
jliempt authored May 7, 2024
1 parent f5fbb7e commit 1f4010a
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions cmd/gcpPublishEvent.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
piperConfig "github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/events"
"github.com/SAP/jenkins-library/pkg/gcp"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/orchestrator"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/vault"

"github.com/pkg/errors"
)
Expand All @@ -19,6 +21,7 @@ type gcpPublishEventUtils interface {

type gcpPublishEventUtilsBundle struct {
config *gcpPublishEventOptions
*vault.Client
}

func (g gcpPublishEventUtilsBundle) GetConfig() *gcpPublishEventOptions {
Expand All @@ -33,17 +36,34 @@ func (g gcpPublishEventUtilsBundle) Publish(projectNumber string, topic string,
return gcp.Publish(projectNumber, topic, token, key, data)
}

// to be implemented through another PR!
func (g gcpPublishEventUtilsBundle) GetOIDCTokenByValidation(roleID string) (string, error) {
return "testToken", nil
}

func gcpPublishEvent(config gcpPublishEventOptions, telemetryData *telemetry.CustomData) {
vaultCreds := piperConfig.VaultCredentials{
AppRoleID: GeneralConfig.VaultRoleID,
AppRoleSecretID: GeneralConfig.VaultRoleSecretID,
VaultToken: GeneralConfig.VaultToken,
}
vaultConfig := map[string]interface{}{
"vaultNamespace": config.VaultNamespace,
"vaultServerUrl": config.VaultServerURL,
}

client, err := piperConfig.GetVaultClientFromConfig(vaultConfig, vaultCreds)
if err != nil {
log.Entry().WithError(err).Warnf("could not create Vault client")
}
defer client.MustRevokeToken()

vaultClient, ok := client.(vault.Client)
if !ok {
log.Entry().WithError(err).Warnf("could not create Vault client")
}

utils := gcpPublishEventUtilsBundle{
config: &config,
Client: &vaultClient,
}

err := runGcpPublishEvent(utils)
err = runGcpPublishEvent(utils)
if err != nil {
// do not fail the step
log.Entry().WithError(err).Warnf("step execution failed")
Expand All @@ -66,10 +86,7 @@ func runGcpPublishEvent(utils gcpPublishEventUtils) error {
return errors.Wrap(err, "failed to create event data")
}

// this is currently returning a mock token. function will be implemented through another PR!
// roleID will come from GeneralConfig.HookConfig.OIDCConfig.RoleID
roleID := "test"
oidcToken, err := utils.GetOIDCTokenByValidation(roleID)
oidcToken, err := utils.GetOIDCTokenByValidation(GeneralConfig.HookConfig.OIDCConfig.RoleID)
if err != nil {
return errors.Wrap(err, "failed to get OIDC token")
}
Expand Down

0 comments on commit 1f4010a

Please sign in to comment.