Skip to content

CxOne: support system trust #5304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/checkmarxOneExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func Authenticate(config checkmarxOneExecuteScanOptions, influx *checkmarxOneExe
if err != nil {
log.Entry().WithError(err).Warning("Failed to get GitHub client")
}
sys, err := checkmarxOne.NewSystemInstance(client, config.ServerURL, config.IamURL, config.Tenant, config.APIKey, config.ClientID, config.ClientSecret)
sys, err := checkmarxOne.NewSystemInstance(client, config.ServerURL, config.IamURL, config.Tenant, config.APIKey, config.ClientID, config.ClientSecret, config.AccessToken)
if err != nil {
return checkmarxOneExecuteScanHelper{}, fmt.Errorf("failed to create Checkmarx One client talking to URLs %v and %v with tenant %v: %s", config.ServerURL, config.IamURL, config.Tenant, err)
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/checkmarxOneExecuteScan_generated.go

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

18 changes: 14 additions & 4 deletions pkg/checkmarxone/checkmarxone.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ type SystemInstance struct {
APIKey string // New for Cx1
oauth_client_id string // separate from APIKey
oauth_client_secret string //separate from APIKey
accessToken string
client piperHttp.Uploader
logger *logrus.Entry
}
Expand Down Expand Up @@ -338,7 +339,7 @@ type System interface {

// NewSystemInstance returns a new Checkmarx client for communicating with the backend
// Updated for Cx1
func NewSystemInstance(client piperHttp.Uploader, serverURL, iamURL, tenant, APIKey, client_id, client_secret string) (*SystemInstance, error) {
func NewSystemInstance(client piperHttp.Uploader, serverURL, iamURL, tenant, APIKey, client_id, client_secret, accessToken string) (*SystemInstance, error) {
loggerInstance := log.Entry().WithField("package", "SAP/jenkins-library/pkg/checkmarxOne")
sys := &SystemInstance{
serverURL: serverURL,
Expand All @@ -347,27 +348,36 @@ func NewSystemInstance(client piperHttp.Uploader, serverURL, iamURL, tenant, API
APIKey: APIKey,
oauth_client_id: client_id,
oauth_client_secret: client_secret,
accessToken: accessToken,
client: client,
logger: loggerInstance,
}

var token string
var err error

if APIKey != "" {
var authModel string

if accessToken != "" { // use access token if provided by the system trust API
authModel = "SystemTrust"
tokenType := "Bearer "
token = tokenType + accessToken
} else if APIKey != "" {
authModel = "APIKey"
token, err = sys.getAPIToken()
if err != nil {
return sys, errors.Wrap(err, fmt.Sprintf("Error fetching oAuth token using API Key: %v", shortenGUID(APIKey)))
}
} else if client_id != "" && client_secret != "" {
authModel = "OAuth2"
token, err = sys.getOAuth2Token()
if err != nil {
return sys, errors.Wrap(err, fmt.Sprintf("Error fetching oAuth token using OIDC client: %v/%v", shortenGUID(client_id), shortenGUID(client_secret)))
}
} else {
return sys, errors.New("No APIKey or client_id/client_secret provided.")
return sys, errors.New("No APIKey or client_id/client_secret or system trust token provided.")
}

sys.logger.Debugf("Created access token from %v", authModel)
log.RegisterSecret(token)

options := piperHttp.ClientOptions{
Expand Down
4 changes: 2 additions & 2 deletions pkg/checkmarxone/checkmarxone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestGetOAuthToken(t *testing.T) {
opts := piperHttp.ClientOptions{}
t.Run("test success", func(t *testing.T) {
myTestClient := senderMock{responseBody: `{"token_type":"Bearer","access_token":"abcd12345","expires_in":7045634}`, httpStatusCode: 200}
sys, _ := NewSystemInstance(&myTestClient, "https://cx1.server.com", "https://cx1iam.server.com", "tenant", "", "client", "secret")
sys, _ := NewSystemInstance(&myTestClient, "https://cx1.server.com", "https://cx1iam.server.com", "tenant", "", "client", "secret", "")
myTestClient.SetOptions(opts)

token, err := sys.getOAuth2Token()
Expand All @@ -143,7 +143,7 @@ func TestGetOAuthToken(t *testing.T) {

t.Run("test new system", func(t *testing.T) {
myTestClient := senderMock{responseBody: `{"token_type":"Bearer","access_token":"abcd12345","expires_in":7045634}`, httpStatusCode: 200}
_, err := NewSystemInstance(&myTestClient, "https://cx1.server.com", "https://cx1iam.server.com", "tenant", "", "client", "secret")
_, err := NewSystemInstance(&myTestClient, "https://cx1.server.com", "https://cx1iam.server.com", "tenant", "", "client", "secret", "")

assert.NoError(t, err, "Error occurred but none expected")
assert.Equal(t, "https://cx1iam.server.com/auth/realms/tenant/protocol/openid-connect/token", myTestClient.urlCalled, "Called url incorrect")
Expand Down
11 changes: 11 additions & 0 deletions resources/metadata/checkmarxOneExecuteScan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ spec:
- type: vaultSecret
name: checkmarxOneVaultSecretName
default: checkmarxOne
- name: accessToken
type: string
description: Token for APIs access. This parameter is used uniquely for the integration with Trust Engine API and the value will be filled automatically by the system. Outside this usecase, OAuth client (clientId/clientSecret) or APIKey must be used for authentication.
mandatory: true
scope:
- PARAMETERS
secret: true
resourceRef:
- type: systemTrustSecret
name: cxoneSystemtrustSecretName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the param name should be just cxoneSystemTrustSecret

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hubadr apparentlythis is the naming convention in Piper. The postfix "SecretName" is to indicate the name of secret in Vault/Jenkins (and now Trust), see https://github.com/search?q=repo%3ASAP%2Fjenkins-library%20SecretName&type=code

default: cxone
- name: preset
type: string
description: The preset to use for scanning, if not set explicitly the step will attempt to look up the project's setting based on the availability of `checkmarxOneCredentialsId`
Expand Down
Loading