This repository was archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Add withAzureCredentials step #1048
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import static org.junit.Assert.assertTrue | ||
|
|
||
| class WithAzureCredentialsStepTests extends ApmBasePipelineTest { | ||
|
|
||
| @Override | ||
| @Before | ||
| void setUp() throws Exception { | ||
| super.setUp() | ||
| script = loadScript('vars/withAzureCredentials.groovy') | ||
| env.HOME = '/foo' | ||
| } | ||
|
|
||
| @Test | ||
| void test_default_parameters() throws Exception { | ||
| def isOK = false | ||
| script.call { | ||
| isOK = true | ||
| } | ||
| printCallStack() | ||
| assertTrue(isOK) | ||
| assertTrue(assertMethodCallContainsPattern('dir', '/foo')) | ||
| assertTrue(assertMethodCallContainsPattern('writeFile', '"clientId": "client_id_1"')) | ||
| assertTrue(assertMethodCallContainsPattern('sh', 'rm .credentials.json')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_default_parameters_on_windows() throws Exception { | ||
| helper.registerAllowedMethod('isUnix', [ ], { false }) | ||
| def isOK = false | ||
| script.call { | ||
| isOK = true | ||
| } | ||
| printCallStack() | ||
| assertTrue(isOK) | ||
| assertTrue(assertMethodCallContainsPattern('bat', 'del .credentials.json')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_all_parameters() throws Exception { | ||
| def isOK = false | ||
| script.call(path: '/bar', credentialsFile: 'mytoken', secret: VaultSecret.SECRET_AZURE.toString()) { | ||
| isOK = true | ||
| } | ||
| printCallStack() | ||
| assertTrue(isOK) | ||
| assertTrue(assertMethodCallContainsPattern('dir', '/bar')) | ||
| assertTrue(assertMethodCallContainsPattern('writeFile', '"clientId": "client_id_1"')) | ||
| assertTrue(assertMethodCallContainsPattern('sh', 'rm mytoken')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_body_error() throws Exception { | ||
| try { | ||
| script.call { | ||
| throw new Exception('Mock an error') | ||
| } | ||
| } catch(e){ | ||
| //NOOP | ||
| } | ||
| printCallStack() | ||
| assertTrue(assertMethodCallContainsPattern('error', 'withAzureCredentials: error')) | ||
| assertTrue(assertMethodCallContainsPattern('sh', 'rm .credentials.json')) | ||
| assertJobStatusFailure() | ||
| } | ||
|
|
||
| @Test | ||
| void test_secret_error() throws Exception { | ||
| try { | ||
| script.call(secret: VaultSecret.SECRET_ERROR.toString()){ | ||
| //NOOP | ||
| } | ||
| } catch(e){ | ||
| //NOOP | ||
| } | ||
| printCallStack() | ||
| assertTrue(assertMethodCallContainsPattern('error', 'withAzureCredentials: Unable to get credentials from the vault: Error message')) | ||
| assertJobStatusFailure() | ||
| } | ||
|
|
||
| @Test | ||
| void test_secret_not_found() throws Exception { | ||
| try{ | ||
| script.call(secret: 'secretNotExists'){ | ||
| //NOOP | ||
| } | ||
| } catch(e){ | ||
| //NOOP | ||
| } | ||
| printCallStack() | ||
| assertTrue(assertMethodCallContainsPattern('error', 'withAzureCredentials: was not possible to get authentication info')) | ||
| assertJobStatusFailure() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| /** | ||
| Wrap the .credentials.json token | ||
| withAzureCredentials(path: '/foo', credentialsFile: '.credentials.json') { | ||
| // block | ||
| } | ||
| */ | ||
|
|
||
| def call(Map args = [:], Closure body) { | ||
| def path = args.containsKey('path') ? args.path : env.HOME | ||
| def credentialsFile = args.containsKey('credentialsFile') ? args.credentialsFile : '.credentials.json' | ||
| def secret = args.containsKey('secret') ? args.secret : 'secret/apm-team/ci/apm-agent-dotnet-azure' | ||
|
|
||
| def props = getVaultSecret(secret: secret) | ||
| if (props?.errors) { | ||
| error "withAzureCredentials: Unable to get credentials from the vault: ${props.errors.toString()}" | ||
| } | ||
| def client_id = props?.data?.client_id | ||
| def client_secret = props?.data?.client_secret | ||
| def subscription_id = props?.data?.subscription_id | ||
| def tenant_id = props?.data?.tenant_id | ||
| if (client_id == null || client_secret == null || subscription_id == null || tenant_id == null) { | ||
| error 'withAzureCredentials: was not possible to get authentication info' | ||
| } | ||
| dir(path) { | ||
| writeFile file: credentialsFile, text: """ | ||
| { | ||
| "clientId": "${client_id}", | ||
| "clientSecret": "${client_secret}", | ||
| "subscriptionId": "${subscription_id}", | ||
| "tenantId": "${tenant_id}" | ||
| } | ||
| """ | ||
| } | ||
| try { | ||
| body() | ||
| } catch(err) { | ||
| error("withAzureCredentials: error ${err.toString()}") | ||
| } finally { | ||
| // ensure any sensitive details are deleted | ||
| dir(path) { | ||
| if (fileExists("${credentialsFile}")) { | ||
| if(isUnix()){ | ||
| sh "rm ${credentialsFile}" | ||
| } else { | ||
| bat "del ${credentialsFile}" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Wrap the azure credentials | ||
|
|
||
| ``` | ||
| withAzureCredentials() { | ||
| // block | ||
| } | ||
|
|
||
| withAzureCredentials(path: '/foo', credentialsFile: '.credentials.json') { | ||
| // block | ||
| } | ||
| ``` | ||
|
|
||
| * path: root folder where the credentials file will be stored. (Optional). Default: ${HOME} env variable | ||
| * credentialsFile: name of the file with the credentials. (Optional). Default: .credentials.json | ||
| * secret: Name of the secret on the the vault root path. (Optional). Default: 'secret/apm-team/ci/apm-agent-dotnet-azure' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we set the permissions of the file to 0600?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
writeFiledoes not support permissions yet (https://issues.jenkins.io/browse/JENKINS-47747) I'm a bit reluctant to add a sh/bat step only for that. Do you see any issues with the existing approach?