Skip to content

Commit 9914045

Browse files
#23 Add support for Azure Key Vault
Signed-off-by: Matthew DeVenny <matt@boxboat.com>
1 parent d1a42eb commit 9914045

File tree

7 files changed

+112
-3
lines changed

7 files changed

+112
-3
lines changed

resources/com/boxboat/jenkins/config.example.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ awsProfileMap:
33
region: us-east-1
44
accessKeyIdCredential: aws-access-key-id
55
secretAccessKeyCredential: aws-secret-access-key
6+
azureProfileMap:
7+
default:
8+
keyVaultName: your-keyvault-name
9+
tenantIdCredential: azure-tenant-id
10+
clientIdCredential: azure-client-id
11+
clientSecretKeyCredential: azure-client-secret-key
612
deployTargetMap:
713
dev01: !!com.boxboat.jenkins.library.deployTarget.KubernetesDeployTarget
814
contextName: boxboat
@@ -61,6 +67,7 @@ vaultMap:
6167
secretIdCredential: vault-secret-id
6268
tokenCredential: vault-token
6369
url: http://localhost:8200
70+
6471
repo:
6572
common:
6673
defaultBranch: master
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.boxboat.jenkins.library.azure
2+
3+
import com.boxboat.jenkins.library.config.BaseConfig
4+
import com.boxboat.jenkins.library.config.Config
5+
6+
class AzureProfile extends BaseConfig<AzureProfile> implements Serializable{
7+
8+
String keyVaultName
9+
10+
String tenantIdCredential
11+
12+
String clientIdCredential
13+
14+
String clientSecretKeyCredential
15+
16+
def withCredentials(Closure closure) {
17+
List<Object> credentials = []
18+
if (tenantIdCredential) {
19+
credentials.add(Config.pipeline.string(credentialsId: tenantIdCredential, variable: 'AZURE_TENANT_ID',))
20+
}
21+
if (clientIdCredential) {
22+
credentials.add(Config.pipeline.string(credentialsId: clientIdCredential, variable: 'AZURE_CLIENT_ID',))
23+
}
24+
if (clientSecretKeyCredential) {
25+
credentials.add(Config.pipeline.string(credentialsId: clientSecretKeyCredential, variable: 'AZURE_CLIENT_SECRET',))
26+
}
27+
Config.pipeline.withCredentials(credentials) {
28+
closure()
29+
}
30+
}
31+
}

src/com/boxboat/jenkins/library/config/GlobalConfig.groovy

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.boxboat.jenkins.library.config
22

33
import com.boxboat.jenkins.library.aws.AwsProfile
4+
import com.boxboat.jenkins.library.azure.AzureProfile
45
import com.boxboat.jenkins.library.deployTarget.IDeployTarget
56
import com.boxboat.jenkins.library.docker.Registry
67
import com.boxboat.jenkins.library.environment.Environment
@@ -37,6 +38,17 @@ class GlobalConfig extends BaseConfig<GlobalConfig> implements Serializable {
3738
return awsProfile
3839
}
3940

41+
AzureProfile getAzureProfile(String key) {
42+
def azureProfile = azureProfileMap.get(key)
43+
if (!azureProfile) {
44+
throw new Exception("azureProfile entry '${key}' does not exist in config file")
45+
}
46+
return azureProfile
47+
48+
}
49+
50+
Map<String, AzureProfile> azureProfileMap
51+
4052
IDeployTarget getDeployTarget(String key) {
4153
def deployTarget = deployTargetMap.get(key)
4254
if (!deployTarget) {
@@ -75,6 +87,4 @@ class GlobalConfig extends BaseConfig<GlobalConfig> implements Serializable {
7587
throw new Exception("vaultKey entry '${key}' does not exist in config file")
7688
}
7789
return vault
78-
}
79-
80-
}
90+
}

src/com/boxboat/jenkins/pipeline/common/dockcmd/DockcmdGetSecrets.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.boxboat.jenkins.pipeline.common.dockcmd
22

3+
import com.boxboat.jenkins.library.azure.AzureProfile
34
import com.boxboat.jenkins.library.config.Config
45
import com.boxboat.jenkins.library.aws.AwsProfile
56
import com.boxboat.jenkins.library.vault.Vault
@@ -10,6 +11,8 @@ class DockcmdGetSecrets implements Serializable {
1011

1112
public String vaultKey
1213

14+
public String azureProfileKey
15+
1316
public String directory = "."
1417

1518
public String[] files = []
@@ -37,6 +40,27 @@ class DockcmdGetSecrets implements Serializable {
3740

3841
}
3942

43+
public parseAzureSecrets(Map<String, Object> additionalOptions = [:]) {
44+
if (!azureProfileKey) {
45+
Config.pipeline.error "'azureProfileKey' is required"
46+
}
47+
AzureProfile azure = Config.global.getAzureProfile(azureProfileKey)
48+
azure.withCredentials {
49+
Config.pipeline.sh parseAzureSecretsScript(azure.keyVaultName, additionalOptions)
50+
}
51+
}
52+
53+
public parseAzureSecretsScript(String keyVaultName, Map<String, Object> additionalOptions = [:]) {
54+
def combinedOptions = combineOptions(options, additionalOptions)
55+
return """
56+
dockcmd_current_dir=\$(pwd)
57+
cd "${directory}"
58+
dockcmd azure get-secrets --key-vault "${keyVaultName}" ${optionsString(combinedOptions)} ${files.join('" "')}
59+
cd "\$dockcmd_current_dir"
60+
"""
61+
62+
}
63+
4064
public parseVaultSecrets(Map<String, Object> additionalOptions = [:]) {
4165
if (!vaultKey) {
4266
Config.pipeline.error "'vaultKey' is required"

test-resources/com/boxboat/jenkins/test/library/config/globalConfig/test.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ awsProfileMap:
33
region: us-east-1
44
accessKeyIdCredential: aws-access-key-id
55
secretAccessKeyCredential: aws-secret-access-key
6+
azureProfileMap:
7+
default:
8+
keyVaultName: vault-name
9+
tenantIdCredential: azure-tenant-id
10+
clientIdCredential: azure-client-id
11+
clientSecretKeyCredential: azure-client-secret-key
612
deployTargetMap:
713
dev01: !!com.boxboat.jenkins.library.deployTarget.KubernetesDeployTarget
814
contextName: boxboat

test-resources/com/boxboat/jenkins/test/pipeline/deployment.jenkins

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ def execute() {
6161

6262
dockcmdAws.parseAwsSecrets()
6363

64+
def dockcmdAzure = new DockcmdGetSecrets(
65+
azureProfileKey: "default",
66+
files: [
67+
"secret-values-*.yaml",
68+
],
69+
options: [
70+
"edit-in-place": true,
71+
"set": [
72+
"Deployment=dev",
73+
"Foo=bar",
74+
]
75+
],
76+
)
77+
78+
dockcmdAzure.parseAzureSecrets()
79+
80+
6481
deploy.withCredentials() {
6582
sh "helm upgrade --install test ."
6683
}

test/com/boxboat/jenkins/test/library/config/GlobalConfigTest.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.boxboat.jenkins.test.library.config
22

33
import com.boxboat.jenkins.library.aws.AwsProfile
4+
<<<<<<< HEAD
5+
=======
6+
import com.boxboat.jenkins.library.azure.AzureProfile
7+
import com.boxboat.jenkins.library.notify.SlackJenkinsAppNotifyTarget
8+
import com.boxboat.jenkins.library.vault.Vault
9+
>>>>>>> #23 Add support for Azure Key Vault
410
import com.boxboat.jenkins.library.config.CommonConfig
511
import com.boxboat.jenkins.library.config.DeployConfig
612
import com.boxboat.jenkins.library.config.GlobalConfig
@@ -68,6 +74,14 @@ class GlobalConfigTest {
6874
secretAccessKeyCredential: "aws-secret-access-key",
6975
),
7076
],
77+
azureProfileMap: [
78+
"default": new AzureProfile(
79+
keyVaultName: "vault-name",
80+
tenantIdCredential: "tenant-id",
81+
clientIdCredential: "azure-client-id",
82+
clientSecretKeyCredential: "azure-client-secret-key",
83+
),
84+
],
7185
deployTargetMap: [
7286
"dev01" : new KubernetesDeployTarget(
7387
contextName: "boxboat",

0 commit comments

Comments
 (0)