Thông thường, khi nhắc đến tích hợp các dịch vụ bên thứ ba với AWS chúng ta thường nghĩ đến việc sử dụng CLI với xác thực access_key và secret_key.
TRong bài viết này, mình xin được chia sẽ một các khác đơn giãn hơn trong việc quản cấu hình và quản trị user, cũng như bảo mật hơn cách làm truyền thống.
Vậy OIDC là gì?
OpenID Connect 1.0 is a simple identity layer on top of the OAuth 2.0 protocol. It allows Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the End-User in an interoperable and REST-like manner.
From AWS
IAM OIDC identity providers are entities in IAM that describe an external identity provider (IdP) service that supports the OpenID Connect (OIDC) standard, such as Google or Salesforce. You use an IAM OIDC identity provider when you want to establish trust between an OIDC-compatible IdP and your AWS account. This is useful when creating a mobile app or web application that requires access to AWS resources, but you don't want to create custom sign-in code or manage your own user identities.
Tạo OIDC
Bạn có thể truy cập vào AWS để tạo, nhưng trong bìa viết này, mình sẻ sử dụng Cloudformation
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
GitLabThumbprint:
Type: String
GitLabProject:
Type: String
Resources:
GitLabOIDCProvider:
Type: AWS::IAM::OIDCProvider
Properties:
Url: https://gitlab.com
ClientIdList:
- https://gitlab.com
ThumbprintList:
- !Ref GitLabThumbprint
GitLabRunnerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Federated: !Sub arn:aws:iam::${AWS::AccountId}:oidc-provider/gitlab.com
Action: sts:AssumeRoleWithWebIdentity
Condition:
StringEquals:
gitlab.com:sub: !Sub project_path:${GitLabProject}:ref_type:branch:ref:main
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/AmazonVPCFullAccess"
- "arn:aws:iam::aws:policy/AmazonS3FullAccess"
- "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"
Outputs:
GitLabRunnerRoleArn:
Value: !GetAtt GitLabRunnerRole.Arn
Phần param GitlabThumbprint bạn có thể cung cấp thông qua hướng dẫn ở đây
Câu hình Gitlab pipeline
Sau khi bạn triển khai ngăn xếp CFN, bạn có thể nhận được vai trò arn ở đầu ra và bạn phải tạo biến có tên ROLE_ARN với vai trò đó.
Truy cập vào temporary credentials
image:
name: amazon/aws-cli:latest
entrypoint:
- '/usr/bin/env'
assume role:
script:
- >
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s"
$(aws sts assume-role-with-web-identity
--role-arn ${ROLE_ARN}
--role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}"
--web-identity-token $CI_JOB_JWT_V2
--duration-seconds 3600
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]'
--output text))
- aws sts get-caller-identity
- aws s3 ls