Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

amazon web services - copy the filecontent fetching from aws ssm parameter to the container using codebuild

Below is my jenkins file, one of the purpose of this pipeline is fetching key files from ssm parameter and copies to a variable.

// Run CodeBuild on  account
pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
    }
    stages {
        stage('TEST') {
            steps {
                script {
                    // Getting SSM Parameters
                    withAWSParameterStore(
                        credentialsId: '1023564897565',
                        regionName: 'us-east-1',
                        recursive: true,
                        naming: 'relative',
                        path: '/ddc/pvs/ops1/ans-wer-tst/'
                    ) {
                        SSH_PRIV_US_KEY = "${env.TLS_PRIVATE_KEY}"
                    }
                    withAWSParameterStore(
                        credentialsId: '1023564897565',
                        regionName: 'eu-west-1',
                        recursive: true,
                        naming: 'relative',
                        path: '/ddc/pvs/ops2/ans-wer-tst/'
                    ) {
                        SSH_PRIV_EU_KEY = "${env.TLS_PRIVATE_KEY}"
                    }
                }

                wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: "${SSH_PRIV_US_KEY}", var: 'VALUE'],[password: "${SSH_PRIV_EU_KEY}", var: 'VALUE']]]) {
                    withAWS(role: "${DDC_CODEBUILD_PVS_ROLE}", roleAccount: "${DDC_PVS_AWS_ACCOUNT_ID}") {
                        awsCodeBuild(
                            projectName: "${DDC_CODEBUILD_PVS_OPS1_VPC}",
                            credentialsType: 'keys',
                            region: "us-east-1",
                            sourceControlType: 'jenkins',
                            buildSpecFile: "buildspec.yml",
                            imageOverride: "1023564897565.dkr.ecr.us-east-1.amazonaws.com/pvs-ops1-ecr-anr-0e47e200ddff4875:0.1",
                            privilegedModeOverride: 'True',
                            envVariables: """[
                                { SSH_PRIV_US_KEY, ${SSH_PRIV_US_KEY} },
                                { SSH_PRIV_EU_KEY, ${SSH_PRIV_EU_KEY} }
                            ]"""
                        )
                    }
                }
            }
        }
    }
}

Below bash script copies the key that is stored in a variable(happened from jenkins file) to the file in container.

#!/bin/bash
###
### CodeBuild script
###
set -e




echo "Get SSH_PUB_KEY"

echo "Get SSH_PRIV_KEY"




#export SSH_PRIV_US_KEY SSH_PRIV_EU_KEY

mkdir -p ~/.ssh
touch ~/.ssh/id_rsa_us   
touch ~/.ssh/id_rsa_eu
chmod 400 ~/.ssh/id_rsa_us
chmod 400 ~/.ssh/id_rsa_eu

echo ${SSH_PRIV_US_KEY} > ~/.ssh/id_rsa_us
echo ${SSH_PRIV_EU_KEY} > ~/.ssh/id_rsa_eu

Issues:

From jenkins output: Below is the jenkins output.

  1. The full key is assigned to the variable(SSH_PRIV_US_KEY and SSH_PRIV_EU_KEY)
  2. when pasting to the file, it doesn't paste the full key, it paste the partial key. What I am doing wrong here?
> environment variables: [
                                { SSH_PRIV_US_KEY, -----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
 },
                                { SSH_PRIV_EU_KEY, -----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
 }
                            ]
    > image: 1023564897565.dkr.ecr.us-east-1.amazonaws.com/pvs-ops1-ecr-anr-0e47e200ddff4875:0.1
    > privileged mode override: True
    > build spec: 
buildspec.yml
[AWS CodeBuild Plugin] 
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Phase is DOWNLOAD_SOURCE
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 CODEBUILD_SRC_DIR=/codebuild/output/src301168615/src
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 YAML location is /codebuild/output/src301168615/src/buildspec.yml
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Processing environment variables
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Moving to directory /codebuild/output/src301168615/src
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Registering with agent
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Phases found in YAML: 1
[AWS CodeBuild Plugin] 2021/01/29 06:12:14  BUILD: 7 commands
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Phase context status code:  Message: 
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Entering phase INSTALL
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Phase complete: INSTALL State: SUCCEEDED
[AWS CodeBuild Plugin] 2021/01/29 06:12:14 Phase context status code:  Message: 
[AWS CodeBuild Plugin] 2021/01/29 06:12:15 Entering phase PRE_BUILD
[AWS CodeBuild Plugin] 2021/01/29 06:12:15 Phase complete: PRE_BUILD State: SUCCEEDED
[AWS CodeBuild Plugin] 2021/01/29 06:12:15 Phase context status code:  Message: 
[AWS CodeBuild Plugin] 2021/01/29 06:12:15 Entering phase BUILD
[AWS CodeBuild Plugin] 2021/01/29 06:12:15 Running command bash codebuild.sh
[AWS CodeBuild Plugin] Get SSH_PUB_KEY
[AWS CodeBuild Plugin] Get SSH_PRIV_KEY
[AWS CodeBuild Plugin] id_rsa_eu
[AWS CodeBuild Plugin] id_rsa_us
[AWS CodeBuild Plugin] -----BEGIN RSA PRIVATE KEY-----WRZSuRtEVVexWa02QRweK+PHxwx9QJJUzSTF5P5xKNC7eOM1Xku+253pt3SR/c+343+b2Vvx5b0T2A8EZiZsSb+pgebrHfe5xeoKGtdTyW1uTYXL6oNSnXMJh3UQkctfszBxXVNZychGrPa/ldyvPM+QvAbr6TUBY4XiWimtXE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7mmNNyse9AloD+HtXLMvAfasZ5kIW0q5pjZIm/nSjId95G997mHBM8vPhbogqeh6rXHFSt0jg4qHvKQAjNCwZV0M6Cz6JGdESMWS1vwG2Ya7xez53I1M55ju3MJ+VZz8fgLSg+iV/Axkhcm+28qOfCvdhrflwKSb9vulanpP76DBf2Y2Mo19JscKUd0Mikc0xxo68nBKu1f5KDvEMReNk3FpTpN/ziQ6BhqqwKCAQEA402K2qftDOZFWIexaPgCqeNyp83Kn79dyf2ontBjkcEZNuD/lal2tqK6fQDrFVbmiTTXbp0uPk5GXgY5tD1eji4HKJW4XCKmUOBBOP5gq1BUafJ3MzHo3hUii7tHoDh7W+YsUK4w4ZLihb15bIKHlB102MFnMO7+sWIHAOkcjTjZixh30Tob+BGKXhLHNMejFoIIDez+/tOKpY4/NAHPgCLcHwU9n...

buildspec.yml

version: 0.2
phases:
  build:
    commands:
      - bash codebuild.sh
      - /bin/bash setup.sh
echo "Get SSH_PRIV_US_KEY"
aws --profile "DEC" --region ${REGION} ssm get-parameter --with-decryption --name /xxx/xxx/tls_private_key --query "Parameter.Value" --output text > ~/.ssh/id_rsa_us
echo "Get SSH_PRIV_EU_KEY"
aws --profile "DEC" --region ${EU_REGION} ssm get-parameter --with-decryption --name /xxx/xxx/private_key --query "Parameter.Value" --output text > ~/.ssh/id_rsa_eu
question from:https://stackoverflow.com/questions/65949946/copy-the-filecontent-fetching-from-aws-ssm-parameter-to-the-container-using-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not directly answering your question but is there any particular reason why you are getting those creds in jenkins and then passing them as an env var to CodeBuild in stead of getting them directly in the CodeBuild buildspec itself by using the built-in parameter store option:

  shell: shell-tag
  variables:
    key: "value"
    key: "value"
  parameter-store:
    key: "value"
    key: "value"
  exported-variables:
    - variable
    - variable
  secrets-manager:
    key: secret-id:json-key:version-stage:version-id
  git-credential-helper: no | yes

From here.

P.S. Sharing ssh keys on the public Internet is not a good practice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...