mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-04 18:23:49 +08:00
ECR auth for push-only operation + code refactoring (#144)
This commit is contained in:
+65
-33
@@ -891,6 +891,60 @@ func createECRSession(region, accessKey, secretKey, sessionToken string) *ecrv1.
|
|||||||
return ecrv1.New(sess)
|
return ecrv1.New(sess)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getECRCredentials(region, registry, assumeRole, externalId, accessKey, secretKey, oidcToken string) (string, string, error) {
|
||||||
|
if assumeRole != "" && oidcToken != "" {
|
||||||
|
// For OIDC auth with assume role
|
||||||
|
awsAccessKey, awsSecretKey, awsSessionToken, err := getOidcCreds(oidcToken, assumeRole)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("failed to get OIDC credentials: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create ECR session and get auth info
|
||||||
|
svc := createECRSession(region, awsAccessKey, awsSecretKey, awsSessionToken)
|
||||||
|
username, password, _, err := getAuthInfo(svc)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("failed to get ECR credentials: %w", err)
|
||||||
|
}
|
||||||
|
return username, password, nil
|
||||||
|
} else if assumeRole != "" {
|
||||||
|
// For assume role auth
|
||||||
|
username, password, _, err := getAssumeRoleCreds(region, assumeRole, externalId, "")
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("failed to get ECR credentials: %w", err)
|
||||||
|
}
|
||||||
|
return username, password, nil
|
||||||
|
} else if accessKey != "" && secretKey != "" {
|
||||||
|
// For direct credentials
|
||||||
|
sess := session.Must(session.NewSession(&awsv1.Config{
|
||||||
|
Region: awsv1.String(region),
|
||||||
|
Credentials: credentials.NewStaticCredentials(
|
||||||
|
accessKey,
|
||||||
|
secretKey,
|
||||||
|
"",
|
||||||
|
),
|
||||||
|
}))
|
||||||
|
svc := ecrv1.New(sess)
|
||||||
|
|
||||||
|
username, password, _, err := getAuthInfo(svc)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("failed to get ECR credentials: %w", err)
|
||||||
|
}
|
||||||
|
return username, password, nil
|
||||||
|
} else {
|
||||||
|
// For IAM role auth (default credentials)
|
||||||
|
sess := session.Must(session.NewSession(&awsv1.Config{
|
||||||
|
Region: awsv1.String(region),
|
||||||
|
}))
|
||||||
|
svc := ecrv1.New(sess)
|
||||||
|
|
||||||
|
username, password, _, err := getAuthInfo(svc)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("failed to get ECR credentials: %w", err)
|
||||||
|
}
|
||||||
|
return username, password, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func handlePushOnly(c *cli.Context) error {
|
func handlePushOnly(c *cli.Context) error {
|
||||||
sourceTarPath := c.String("source-tar-path")
|
sourceTarPath := c.String("source-tar-path")
|
||||||
if sourceTarPath == "" {
|
if sourceTarPath == "" {
|
||||||
@@ -913,40 +967,18 @@ func handlePushOnly(c *cli.Context) error {
|
|||||||
return fmt.Errorf("failed to load image from tarball: %v", err)
|
return fmt.Errorf("failed to load image from tarball: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get ECR credentials using existing auth methods
|
// Get ECR credentials using the common function
|
||||||
var username, password string
|
username, password, err := getECRCredentials(
|
||||||
var svc *ecrv1.ECR
|
c.String("region"),
|
||||||
if oidcToken := c.String("oidc-token-id"); oidcToken != "" && c.String("assume-role") != "" {
|
registry,
|
||||||
accessKey, secretKey, sessionToken, err := getOidcCreds(oidcToken, c.String("assume-role"))
|
c.String("assume-role"),
|
||||||
if err != nil {
|
c.String("external-id"),
|
||||||
return fmt.Errorf("failed to get OIDC credentials: %v", err)
|
c.String("access-key"),
|
||||||
}
|
c.String("secret-key"),
|
||||||
|
c.String("oidc-token-id"),
|
||||||
svc = createECRSession(c.String("region"), accessKey, secretKey, sessionToken)
|
)
|
||||||
} else if assumeRole := c.String("assume-role"); assumeRole != "" {
|
|
||||||
accessKey, secretKey, sessionToken, err := getAssumeRoleCreds(c.String("region"), assumeRole, c.String("external-id"), "")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get assume role credentials: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
svc = createECRSession(c.String("region"), accessKey, secretKey, sessionToken)
|
|
||||||
} else {
|
|
||||||
// Use direct credentials or IAM role
|
|
||||||
sess := session.Must(session.NewSession(&awsv1.Config{
|
|
||||||
Region: awsv1.String(c.String("region")),
|
|
||||||
Credentials: credentials.NewStaticCredentials(
|
|
||||||
c.String("access-key"),
|
|
||||||
c.String("secret-key"),
|
|
||||||
"",
|
|
||||||
),
|
|
||||||
}))
|
|
||||||
svc = ecrv1.New(sess)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get ECR auth token using the configured session
|
|
||||||
username, password, _, err = getAuthInfo(svc)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get ECR credentials: %v", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup crane auth
|
// Setup crane auth
|
||||||
|
|||||||
Reference in New Issue
Block a user