kaniko icon indicating copy to clipboard operation
kaniko copied to clipboard

Support native AWS IAM integration with updated docker-credential-ecr-login

Open siwyd opened this issue 7 years ago • 16 comments

There is now native integration with AWS IAM using Kubernetes ServiceAccounts: https://aws.amazon.com/blogs/opensource/introducing-fine-grained-iam-roles-service-accounts/. This requires all SDK's used to interact with AWS to support this however. As we use kaniko to build images on AWS Kubernetes clusters, it would be great if kaniko shipped with a docker-credential-ecr-login binary that supports this native IAM.

I've opened a PR in the amazon-ecr-credential-helper repo to update the SDK: https://github.com/awslabs/amazon-ecr-credential-helper/pull/183.

I'm opening this issue so the kaniko Docker image can get updated with a new binary when a new amazon-ecr-credential-helper gets released.

siwyd avatar Sep 18 '19 08:09 siwyd

@tejal29 After the PR merge (https://github.com/awslabs/amazon-ecr-credential-helper/pull/183), amazon-ecr-credential-helper master branch now supports native Kubernetes AWS IAM integration. Would it be possible perhaps to build a new kaniko release so this is supported in the upstream kaniko container image?

siwyd avatar Oct 28 '19 12:10 siwyd

Is this solved with the new release?

patstrom avatar Nov 18 '19 10:11 patstrom

Can someone confirm that the 0.15.0 is compatible with IRSA ?

antiqe avatar Jan 12 '20 12:01 antiqe

The image at gcr.io/kaniko-project/executor:debug-v0.16.0 does contain the latest release of docker-credential-ecr-login: 0.4.0, from Jan 6th and containing the IRSA feature. However, I still cannot get kaniko to retrieve the correct credentials. I always end up with the Instance Profile credentials instead of those from the Kubernetes service account.

I have verified that the correct AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE values are set according to IRSA. I don't see why I get the instance credentials.

dlaidlaw avatar Feb 11 '20 21:02 dlaidlaw

It works by setting the following environment variable: AWS_SDK_LOAD_CONFIG=true

diist avatar Feb 25 '20 04:02 diist

If running on an EC2 instance with an instance profile, you may also need to set: AWS_EC2_METADATA_DISABLED=true In my case kaniko kept getting the EC2 instance profile role credentials instead. This disables the instance profile role credentials and kaniko then gets the expected credentials.

To be clear, set both AWS_EC2_METADATA_DISABLED and AWS_SDK_LOAD_CONFIG as mentioned above to true on EC2 instances with instance profiles.

dlaidlaw avatar Feb 25 '20 13:02 dlaidlaw

In my case, running the debug image on kubernetes, I had to overwrite the docker config first:

echo '{"credsStore":"ecr-login"}' > /kaniko/.docker/config.json
/kaniko/executor --context `pwd` --destination xxxxxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/app:latest

gsf avatar Mar 16 '20 21:03 gsf

What's the current status of this? I am still struggling to get this to work.

After adding the environment variables AWS_EC2_METADATA_DISABLED and AWS_SDK_LOAD_CONFIG as described in https://github.com/GoogleContainerTools/kaniko/issues/780#issuecomment-590856650, I at least was able to "fix" the EOF issue described in https://github.com/GoogleContainerTools/kaniko/issues/1935.

However, instead I am now greeted with the error message described in https://github.com/GoogleContainerTools/kaniko/issues/1455#issuecomment-805844516.

I have tried setting credsStore in config.json as described by @gsf, as well as setting credHelpers as described in https://github.com/GoogleContainerTools/kaniko/issues/1455#issuecomment-824753853 as well as a combination of both.

I have also tried using the v1.6 release as suggested in https://github.com/GoogleContainerTools/kaniko/issues/1935#issuecomment-1035011274, as well as v1.7 and v1.8, no luck so far.

At this point there has been so much contradicting information accumulated over time that it seems to become impossible to keep track of a working configuration.

Are there currently still folks out there with a working IAM based setup?

Jasper-Ben avatar Mar 26 '22 16:03 Jasper-Ben

I'm not sure it's helpful, but I think it's worth calling out that since v1.8.0 Kaniko includes ECR's cred helper logic even if it's not configured in the Docker config, if the registry looks like an ECR registry and if the necessary env vars are set. So you might be able to remove ECR specific configuration from the Docker config entirely. My hope is that, if this works, this becomes a better option than tweaking esoteric config knobs and everything Just Works.

Longer-term I'd love to have better tests and examples for this to ensure everything works (and remains working), and to remove the cred helpers from the image, and cred helper config from the setup instructions.

If you happen to try this out and things work (or don't), let me know and I'd love to document this a bit better.

imjasonh avatar Mar 26 '22 18:03 imjasonh

I'm not sure it's helpful, but I think it's worth calling out that since v1.8.0 Kaniko includes ECR's cred helper logic even if it's not configured in the Docker config, if the registry looks like an ECR registry and if the necessary env vars are set. So you might be able to remove ECR specific configuration from the Docker config entirely. My hope is that, if this works, this becomes a better option than tweaking esoteric config knobs and everything Just Works.

Longer-term I'd love to have better tests and examples for this to ensure everything works (and remains working), and to remove the cred helpers from the image, and cred helper config from the setup instructions.

If you happen to try this out and things work (or don't), let me know and I'd love to document this a bit better.

Thanks for the heads-up! This indeed makes the whole config less fiddly. I was able to get this to work now. The main issue was a typo in my service account annotations, followed by some weird issues I'd consider a bug in the AWS IAM policy. But that is outside of the scope of this project.

I will create a PR for updating the documentation on this :)

Jasper-Ben avatar Mar 28 '22 11:03 Jasper-Ben

@Jasper-Ben were you able to get this working? I'm still getting this error using Tekton Kaniko task (using AWS ECR IRSA):

error checking push permissions -- make sure you entered the correct tag name, and that you are authenticated correctly, and try again: checking push permission for "xxx.dkr.ecr.us-west-1.amazonaws.com/flyte/flytesnacks-development-my_wf:v0.0.1": Post "https://xxx.dkr.ecr.us-west-1.amazonaws.com/v2/flyte/flytesnacks-development-my_wf/blobs/uploads/": EOF

seunggs avatar Jul 17 '22 02:07 seunggs

@Jasper-Ben were you able to get this working? I'm still getting this error using Tekton Kaniko task (using AWS ECR IRSA):

error checking push permissions -- make sure you entered the correct tag name, and that you are authenticated correctly, and try again: checking push permission for "xxx.dkr.ecr.us-west-1.amazonaws.com/flyte/flytesnacks-development-my_wf:v0.0.1": Post "https://xxx.dkr.ecr.us-west-1.amazonaws.com/v2/flyte/flytesnacks-development-my_wf/blobs/uploads/": EOF

:wave: @seunggs

Yes, I was able to get this working and documented the setup in the project's README.

Summarized, my setup:

  • EC2InstanceProfileForImageBuilderECRContainerBuilds managed IAM policy (I tried using a more fine-granular self-managed policy, limiting permissions to a single ECR but wasn't able to get it working. Maybe a bug in IAM). In my setup this policy is then bound to a EKS Kubernetes serviceaccount using OIDC.
  • Kubernetes pod with the following variables set:
    • AWS_SDK_LOAD_CONFIG: true
    • AWS_EC2_METADATA_DISABLED: true
    • AWS_REGION: <my_aws_region> (not sure this is necessary, if so this should be added to the README)

Hope this helps :slightly_smiling_face:

Jasper-Ben avatar Jul 17 '22 12:07 Jasper-Ben

Hi @Jasper-Ben thank you for this but I still can't get it to work. Do you mind sharing the Kaniko version you're using?

seunggs avatar Jul 18 '22 02:07 seunggs

Sure, we are currently using gcr.io/kaniko-project/executor:v1.8.0.

This is our (incomplete) terraform config, in case it is of any help:

data "aws_caller_identity" "current" {}

locals {
  eks_cluster_oidc_issuer_url = replace(var.eks_cluster_oidc_issuer_url, "https://", "")
  common_labels_k8s = { for k, v in var.tags : lower(k) => v }
  service_account_name = "${var.prefix}${var.project_name}-build"
  s3_object_key = replace(var.path_to_buildfiles_archive, "/^.*\\//", "")
}

# IAM policy granting access to the buildfiles s3 bucket
data "aws_iam_policy_document" "s3_access" {
  statement {
    sid    = "${replace("${var.prefix}${var.project_name}", "/[^0-9A-Za-z]/", "")}S3Access"
    effect = "Allow"
    actions = [
      "s3:GetObject",
      "s3:GetObjectVersion",
      "s3:ListBucket"
    ]
    resources = [
      module.buildfiles_s3_bucket.s3_bucket_arn,
      "${module.buildfiles_s3_bucket.s3_bucket_arn}/*"
    ]
  }
}

resource "aws_iam_policy" "s3_access" {
  name   = "${var.prefix}${var.project_name}-s3_access"
  policy = data.aws_iam_policy_document.s3_access.json
}

# IAM role which will be bound to the Kubernetes service account
module "iam_role" {
  source       = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
  version      = "~> 4.18"
  create_role  = true
  role_name    = "${var.prefix}${var.project_name}-build_${var.eks_cluster_name}"
  provider_url = local.eks_cluster_oidc_issuer_url
  # grant access to ecr repositories and s3 build files
  role_policy_arns = [
    "arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilderECRContainerBuilds",
    aws_iam_policy.s3_access.arn
  ]

  oidc_fully_qualified_audiences = ["sts.amazonaws.com"]
  oidc_fully_qualified_subjects  = ["system:serviceaccount:${var.namespace}:${local.service_account_name}"]
  tags = merge(
    var.tags,
    {
      "Name" = "${var.prefix}${var.project_name}-build_${var.eks_cluster_name}"
    },
  )
}

resource "kubernetes_service_account" "service_account" {
  metadata {
    name      = local.service_account_name
    namespace = var.namespace
    labels    = local.common_labels_k8s
    annotations = {
      "eks.amazonaws.com/role-arn" = module.iam_role.iam_role_arn
    }
  }
}

# build and push container image
resource "kubernetes_job" "build_image" {
  metadata {
    name      = "${var.prefix}${var.project_name}-build"
    namespace = var.namespace
    labels    = local.common_labels_k8s
  }
  spec {
    template {
      metadata {}
      spec {
        container {
          name              = "kaniko"
          image             = "gcr.io/kaniko-project/executor:v1.8.0"
          image_pull_policy = "IfNotPresent"
          args = [
            "--context=s3://${module.buildfiles_s3_bucket.s3_bucket_id}/${local.s3_object_key}",
            "--destination=${var.ecr_repository_url}:${aws_s3_bucket_object.buildfiles.etag}"
          ]
          env {
            name  = "AWS_SDK_LOAD_CONFIG"
            value = true
          }
          env {
            name  = "AWS_EC2_METADATA_DISABLED"
            value = true
          }
          env {
            name  = "AWS_REGION"
            value = var.aws_region
          }
        }
        service_account_name = kubernetes_service_account.service_account.metadata[0].name
        restart_policy       = "Never"
      }
    }
  }
}

Jasper-Ben avatar Jul 18 '22 08:07 Jasper-Ben

Thank you for this! I'll look through it to check and see what the difference may be - been stuck with this for 3 days :(

seunggs avatar Jul 18 '22 15:07 seunggs

@Jasper-Ben were you able to get this working? I'm still getting this error using Tekton Kaniko task (using AWS ECR IRSA):

error checking push permissions -- make sure you entered the correct tag name, and that you are authenticated correctly, and try again: checking push permission for "xxx.dkr.ecr.us-west-1.amazonaws.com/flyte/flytesnacks-development-my_wf:v0.0.1": Post "https://xxx.dkr.ecr.us-west-1.amazonaws.com/v2/flyte/flytesnacks-development-my_wf/blobs/uploads/": EOF

👋 @seunggs

Yes, I was able to get this working and documented the setup in the project's README.

Summarized, my setup:

  • EC2InstanceProfileForImageBuilderECRContainerBuilds managed IAM policy (I tried using a more fine-granular self-managed policy, limiting permissions to a single ECR but wasn't able to get it working. Maybe a bug in IAM). In my setup this policy is then bound to a EKS Kubernetes serviceaccount using OIDC.

  • Kubernetes pod with the following variables set:

    • AWS_SDK_LOAD_CONFIG: true
    • AWS_EC2_METADATA_DISABLED: true
    • AWS_REGION: <my_aws_region> (not sure this is necessary, if so this should be added to the README)

Hope this helps 🙂

This didn't work for me, but this https://github.com/awslabs/amazon-ecr-credential-helper/issues/270#issuecomment-995892423 did.
--ignore-path="$AWS_WEB_IDENTITY_TOKEN_FILE" as Kaniko argument, without any other configuration, Kaniko version 1.23.1

vtstanescu avatar Mar 23 '25 11:03 vtstanescu