Skip to content

Commit

Permalink
feat(events): add step to emit events to GCP (#4901)
Browse files Browse the repository at this point in the history
* feat(gcp): add step to send events to GCP (#4896)

* add gcp token handling

* add initial step

* publish events

* add test cases

* fix test case

---------

Co-authored-by: Jordi van Liempt <[email protected]>

* feat(gcp): Generate and validate the GCP OIDC token (#4899)

* test setup for generation of jwt token

* oidc token generator

* push new step files

* formatted code

* removed toolchain and jose

* removed toolchain:go 1.22.2

---------

Co-authored-by: jliempt <>
Co-authored-by: D071696 <[email protected]>
Co-authored-by: d071696 <[email protected]>

* feat(events): add pipeline start and end event (#4900)

* add gcp token handling

* add initial step

* publish events

* add test cases

* fix test case

* move files

* add possible values

* handle start and end event

* add sap events

* dependencies

---------

Co-authored-by: Jordi van Liempt <[email protected]>

* log successful event publish

* remove dummy step

* prevent step from failing

* improve event creation

* improve event creation

* simplify eventing

* remove detailed events

* update parameter scope

* update go.sum

* fix test case

* add missing method

* refactor OIDC part

* add oidc.go to vault pkg

* mock OIDC token retrieval

* mock GCP functions

* update OIDC function name in Vault mocks

* get event data from CPE

* don't encode data payload in b64

* remove vault related changes

* remove vault changes from step code

* remove commented out code

* documentation/steps/gcpPublishEvent.md

* documentation/steps/gcpPublishEvent.md

* remove hardcoded eventData

* update roleID

* go generate

* add ordering key for pubsub event

---------

Co-authored-by: Jordi van Liempt <[email protected]>
Co-authored-by: D071696 <[email protected]>
Co-authored-by: d071696 <[email protected]>
Co-authored-by: jliempt <>
  • Loading branch information
4 people authored May 6, 2024
1 parent fb809a2 commit 0f83941
Show file tree
Hide file tree
Showing 18 changed files with 961 additions and 76 deletions.
90 changes: 90 additions & 0 deletions cmd/gcpPublishEvent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cmd

import (
"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/pkg/errors"
)

type gcpPublishEventUtils interface {
GetConfig() *gcpPublishEventOptions
GetOIDCTokenByValidation(roleID string) (string, error)
GetFederatedToken(projectNumber, pool, provider, token string) (string, error)
Publish(projectNumber string, topic string, token string, key string, data []byte) error
}

type gcpPublishEventUtilsBundle struct {
config *gcpPublishEventOptions
}

func (g gcpPublishEventUtilsBundle) GetConfig() *gcpPublishEventOptions {
return g.config
}

func (g gcpPublishEventUtilsBundle) GetFederatedToken(projectNumber, pool, provider, token string) (string, error) {
return gcp.GetFederatedToken(projectNumber, pool, provider, token)
}

func (g gcpPublishEventUtilsBundle) Publish(projectNumber string, topic string, token string, key string, data []byte) error {
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) {
utils := gcpPublishEventUtilsBundle{
config: &config,
}

err := runGcpPublishEvent(utils)
if err != nil {
// do not fail the step
log.Entry().WithError(err).Warnf("step execution failed")
}
}

func runGcpPublishEvent(utils gcpPublishEventUtils) error {
config := utils.GetConfig()

var data []byte
var err error

provider, err := orchestrator.GetOrchestratorConfigProvider(nil)
if err != nil {
log.Entry().WithError(err).Warning("Cannot infer config from CI environment")
}

data, err = events.NewEvent(config.EventType, config.EventSource).CreateWithJSONData(config.EventData).ToBytes()
if err != nil {
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)
if err != nil {
return errors.Wrap(err, "failed to get OIDC token")
}

token, err := utils.GetFederatedToken(config.GcpProjectNumber, config.GcpWorkloadIDentityPool, config.GcpWorkloadIDentityPoolProvider, oidcToken)
if err != nil {
return errors.Wrap(err, "failed to get federated token")
}

err = utils.Publish(config.GcpProjectNumber, config.Topic, token, provider.BuildURL(), data)
if err != nil {
return errors.Wrap(err, "failed to publish event")
}

log.Entry().Info("event published successfully!")

return nil
}
255 changes: 255 additions & 0 deletions cmd/gcpPublishEvent_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions cmd/gcpPublishEvent_generated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build unit
// +build unit

package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGcpPublishEventCommand(t *testing.T) {
t.Parallel()

testCmd := GcpPublishEventCommand()

// only high level testing performed - details are tested in step generation procedure
assert.Equal(t, "gcpPublishEvent", testCmd.Use, "command name incorrect")

}
Loading

0 comments on commit 0f83941

Please sign in to comment.