簡體   English   中英

使用 cloudformation 從 eks 集群在 aws 中創建 OIDC 提供程序

[英]Creating the OIDC provider in aws from the eks cluster using cloudformation

我目前正在研究 cloudformation 模板。 該模板通常使用集群自動擴縮器創建 EKS 集群。 在這樣做的過程中,我創建了一個 lambda function,它將自動創建帶有 EKS 集群 Url 的 OIDC 提供程序。 問題是指紋。 我無法為相同的指紋創建導致集群自動縮放器 pod 失敗的指紋。 有什么方法可以讓我們也從 lambda function 創建指紋? 下面是 lambda function 的代碼。 現有的指紋是一個示例。

          import boto3
          import json
          import cfnresponse

       
          def lambda_handler(event, context):
            
            client = boto3.client('iam')
            name=  event['ResourceProperties']['cluster_name']
            responseData= {}
            responseStatus="SUCCESS"
            
            try:
              print("In thetry block")
              if event['RequestType'] == 'Delete':
                print("Request Type:",event['RequestType'])
                print("Delete Request - No Physical resources to delete")
              elif event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
                print("The request type is updated")
                response2 = client.create_open_id_connect_provider(
                        ClientIDList=[
                          'my-application-id',
                        ],
                        ThumbprintList=[
                          '3768084dfb3d2b68b7897bf5f565da8efEXAMPLE',
                        ],
                        Url=fetchClusterOIDC(name),
                        )
                print("The OIDC Created")
                oidc_response_url = fetchClusterOIDC(name)
                oidc_response=oidc_response_url.split("https://")[1]
                
                responseData = {'oidc': oidc_response}

                print("Responsedata Created",responseData)
                print("Request Type:",event['RequestType'])
                print("Sending response to custom resource for event type " + event['RequestType'])
                cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
            except Exception as e:
              print(e)
              responseData = {'Failed': 'Test Failed.'}
              responseStatus="FAILED"
              cfnresponse.send(event, context, cfnresponse.FAILED, responseData)  
          
          def fetchClusterOIDC(cluster_name):
            print("Getting Cluster OIDC value for cluster name "+ cluster_name)
            oidc = ''
            client = boto3.client('eks')
            try:
                response = client.describe_cluster(
                    name=cluster_name
                )
                if response['ResponseMetadata']['HTTPStatusCode'] == 200:
                    print("Success response recieved for describing cluster "+ cluster_name)
                    oidc = (response['cluster']['identity']['oidc']['issuer'])
                    print('OIDC output recieved '+ oidc + ' for Cluster Name ' + cluster_name)
                return oidc
            except Exception as e:
                print('Failed to fetch Cluster OIDC value for cluster name ' + cluster_name, e)

我使用了 aws api 而不是 Lambda function。 cloudformation 腳本在 output 中給出了 OIDC url 和 CertificateAuthority。 之后,我運行 bash 腳本,該腳本自動運行並生成指紋帖子,我們可以使用 Aws API 使用 url 和生成的指紋創建 OIDC 提供程序。

要生成指紋,請點擊以下鏈接: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html

在此,我們可以直接解碼 EKS 集群提供的 CertificateAuthority,而不是執行第 4 步。 解碼命令為:echo -n 'CertificateAuthority'| base64 --解碼

這將生成證書並使您的工作更輕松。

我發現這種方式比創建 lambda function 和生成 OIDC 提供程序要容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM