-
Notifications
You must be signed in to change notification settings - Fork 197
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
Added AWS Credentials Support for Scanning Private Registry #1062
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ package trivy | |
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1" | ||
|
@@ -13,6 +15,10 @@ import ( | |
"github.com/aquasecurity/starboard/pkg/kube" | ||
"github.com/aquasecurity/starboard/pkg/starboard" | ||
"github.com/aquasecurity/starboard/pkg/vulnerabilityreport" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ecr" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
|
@@ -26,6 +32,10 @@ const ( | |
Plugin = "Trivy" | ||
) | ||
|
||
const ( | ||
AWSECR_Image_Regex = "^\\d+\\.dkr\\.ecr\\.(\\w+-\\w+-\\d+)\\.amazonaws\\.com\\/" | ||
) | ||
|
||
const ( | ||
keyTrivyImageRef = "trivy.imageRef" | ||
keyTrivyMode = "trivy.mode" | ||
|
@@ -42,6 +52,7 @@ const ( | |
keyTrivyGitHubToken = "trivy.githubToken" | ||
keyTrivySkipFiles = "trivy.skipFiles" | ||
keyTrivySkipDirs = "trivy.skipDirs" | ||
keyTrivyUseECRRoleCreds = "trivy.useEcrRoleCreds" | ||
|
||
keyTrivyServerURL = "trivy.serverURL" | ||
keyTrivyServerTokenHeader = "trivy.serverTokenHeader" | ||
|
@@ -62,6 +73,11 @@ const ( | |
ClientServer Mode = "ClientServer" | ||
) | ||
|
||
type ecr_credentials struct { | ||
username string | ||
password string | ||
} | ||
|
||
type Command string | ||
|
||
const ( | ||
|
@@ -118,6 +134,11 @@ func (c Config) GetServerURL() (string, error) { | |
return c.GetRequiredData(keyTrivyServerURL) | ||
} | ||
|
||
func (c Config) UseECRCredentials() bool { | ||
_, ok := c.Data[keyTrivyUseECRRoleCreds] | ||
return ok | ||
} | ||
|
||
func (c Config) IgnoreFileExists() bool { | ||
_, ok := c.Data[keyTrivyIgnoreFile] | ||
return ok | ||
|
@@ -547,31 +568,45 @@ func (p *plugin) getPodSpecForStandaloneMode(ctx starboard.PluginContext, config | |
}) | ||
} | ||
|
||
if _, ok := credentials[c.Name]; ok && secret != nil { | ||
registryUsernameKey := fmt.Sprintf("%s.username", c.Name) | ||
registryPasswordKey := fmt.Sprintf("%s.password", c.Name) | ||
if config.UseECRCredentials() { | ||
var aws_creds = GetAuthorizationToken(c.Image) | ||
var creds (ecr_credentials) = ecr_credentials{aws_creds[0][1], aws_creds[0][2]} | ||
|
||
env = append(env, corev1.EnvVar{ | ||
Name: "TRIVY_USERNAME", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
Name: "TRIVY_USERNAME", | ||
Value: creds.username, | ||
}) | ||
env = append(env, corev1.EnvVar{ | ||
Name: "TRIVY_PASSWORD", | ||
Value: creds.password, | ||
}) | ||
} else { | ||
if _, ok := credentials[c.Name]; ok && secret != nil { | ||
registryUsernameKey := fmt.Sprintf("%s.username", c.Name) | ||
registryPasswordKey := fmt.Sprintf("%s.password", c.Name) | ||
|
||
env = append(env, corev1.EnvVar{ | ||
Name: "TRIVY_USERNAME", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
}, | ||
Key: registryUsernameKey, | ||
}, | ||
Key: registryUsernameKey, | ||
}, | ||
}, | ||
}, corev1.EnvVar{ | ||
Name: "TRIVY_PASSWORD", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
}, corev1.EnvVar{ | ||
Name: "TRIVY_PASSWORD", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
}, | ||
Key: registryPasswordKey, | ||
}, | ||
Key: registryPasswordKey, | ||
}, | ||
}, | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
env, err = p.appendTrivyInsecureEnv(config, c.Image, env) | ||
|
@@ -795,31 +830,45 @@ func (p *plugin) getPodSpecForClientServerMode(ctx starboard.PluginContext, conf | |
}, | ||
} | ||
|
||
if _, ok := credentials[container.Name]; ok && secret != nil { | ||
registryUsernameKey := fmt.Sprintf("%s.username", container.Name) | ||
registryPasswordKey := fmt.Sprintf("%s.password", container.Name) | ||
if config.UseECRCredentials() { | ||
var aws_creds = GetAuthorizationToken(container.Image) | ||
var creds (ecr_credentials) = ecr_credentials{aws_creds[0][1], aws_creds[0][2]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check matrix size both row and column to avoid |
||
|
||
env = append(env, corev1.EnvVar{ | ||
Name: "TRIVY_USERNAME", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
Name: "TRIVY_USERNAME", | ||
Value: creds.username, | ||
}) | ||
env = append(env, corev1.EnvVar{ | ||
Name: "TRIVY_PASSWORD", | ||
Value: creds.password, | ||
}) | ||
} else { | ||
if _, ok := credentials[container.Name]; ok && secret != nil { | ||
registryUsernameKey := fmt.Sprintf("%s.username", container.Name) | ||
registryPasswordKey := fmt.Sprintf("%s.password", container.Name) | ||
|
||
env = append(env, corev1.EnvVar{ | ||
Name: "TRIVY_USERNAME", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
}, | ||
Key: registryUsernameKey, | ||
}, | ||
Key: registryUsernameKey, | ||
}, | ||
}, | ||
}, corev1.EnvVar{ | ||
Name: "TRIVY_PASSWORD", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
}, corev1.EnvVar{ | ||
Name: "TRIVY_PASSWORD", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: secret.Name, | ||
}, | ||
Key: registryPasswordKey, | ||
}, | ||
Key: registryPasswordKey, | ||
}, | ||
}, | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
env, err = p.appendTrivyInsecureEnv(config, container.Image, env) | ||
|
@@ -1301,3 +1350,33 @@ func constructEnvVarSourceFromConfigMap(envName, trivyConfigName, trivyConfikey | |
} | ||
return | ||
} | ||
|
||
func GetAuthorizationToken(ImageUrl string) [][]string { | ||
svc := ecr.New(session.New(aws.NewConfig().WithRegion(regexp.MustCompile(AWSECR_Image_Regex).FindAllStringSubmatch(ImageUrl, -1)[0][1]))) | ||
input := &ecr.GetAuthorizationTokenInput{} | ||
|
||
result, err := svc.GetAuthorizationToken(input) | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
case ecr.ErrCodeServerException: | ||
fmt.Println(ecr.ErrCodeServerException, aerr.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better pass logger and use it: |
||
case ecr.ErrCodeInvalidParameterException: | ||
fmt.Println(ecr.ErrCodeInvalidParameterException, aerr.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here use logger |
||
default: | ||
fmt.Println(aerr.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here use logger |
||
} | ||
} else { | ||
fmt.Println(err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ |
||
} | ||
return nil | ||
} | ||
|
||
var credentials = *result.AuthorizationData[0].AuthorizationToken | ||
|
||
sDec, _ := base64.StdEncoding.DecodeString(credentials) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
pattern := regexp.MustCompile("^(AWS):(.+)$") | ||
return pattern.FindAllStringSubmatch(string(sDec), -1) | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check matrix size both row and column to avoid
index out of range