Skip to content

Commit

Permalink
Feature : Customize Secret name using Annotations
Browse files Browse the repository at this point in the history
To change the default name of the secret (originally "namespace-secretname"),
We can now give a custom name to the secret using "jenkins.openshift.io/secret.name"
annotation.

```
annotations:
  jenkins.openshift.io/secret.name: CustomeName
```
  • Loading branch information
waveywaves authored and openshift-merge-robot committed Mar 6, 2019
1 parent e617b32 commit 0136d1c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class Annotations {
public static final String GENERATED_BY_JENKINS = "jenkins";
public static final String DISABLE_SYNC_CREATE = "jenkins.openshift.io/disable-sync-create";
public static final String BUILDCONFIG_NAME = "openshift.io/build-config.name";
public static final String SECRET_NAME = "jenkins.openshift.io/secret.name";
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static synchronized String updateSourceCredentials(BuildConfig buildConfi
String credID = null;
if (sourceSecret != null) {
credID = upsertCredential(sourceSecret, sourceSecret.getMetadata().getNamespace(),
sourceSecret.getMetadata().getName());
sourceSecret.getMetadata().getName(),
sourceSecret.getMetadata().getAnnotations().get(Annotations.SECRET_NAME));
if (credID != null)
BuildConfigSecretToCredentialsMap.linkBCSecretToCredential(NamespaceName.create(buildConfig).toString(),
credID);
Expand Down Expand Up @@ -96,19 +97,19 @@ public static synchronized String upsertCredential(Secret secret) throws IOExcep
if (secret != null) {
ObjectMeta metadata = secret.getMetadata();
if (metadata != null) {
return upsertCredential(secret, metadata.getNamespace(), metadata.getName());
return upsertCredential(secret, metadata.getNamespace(), metadata.getName(), metadata.getAnnotations().get(Annotations.SECRET_NAME));
}
}
return null;
}

private static String upsertCredential(Secret secret, String namespace, String secretName) throws IOException {
private static String upsertCredential(Secret secret, String namespace, String secretName, String customSecretName) throws IOException {
String id = null;
if (secret != null) {
Credentials creds = secretToCredentials(secret);
if (creds == null)
return null;
id = secretName(namespace, secretName);
id = secretName(namespace, secretName, customSecretName);
Credentials existingCreds = lookupCredentials(id);
final SecurityContext previousContext = ACL.impersonate(ACL.SYSTEM);
try {
Expand Down Expand Up @@ -162,7 +163,7 @@ private static void deleteCredential(String id, NamespaceName name, String resou

public static void deleteCredential(Secret secret) throws IOException {
if (secret != null) {
String id = secretName(secret.getMetadata().getNamespace(), secret.getMetadata().getName());
String id = secretName(secret.getMetadata().getNamespace(), secret.getMetadata().getName(), secret.getMetadata().getAnnotations().get(Annotations.SECRET_NAME));
deleteCredential(id, NamespaceName.create(secret), secret.getMetadata().getResourceVersion());
}
}
Expand Down Expand Up @@ -197,8 +198,8 @@ private static Credentials lookupCredentials(String id) {
CredentialsMatchers.withId(id));
}

private static String secretName(String namespace, String name) {
return namespace + "-" + name;
private static String secretName(String namespace, String name, String customName) {
return (customName == null) ? namespace + "-" + name : customName;
}

private static Credentials arbitraryKeyValueTextCredential(Map<String, String> data, String secretName) {
Expand All @@ -225,6 +226,8 @@ private static Credentials arbitraryKeyValueTextCredential(Map<String, String> d
private static Credentials secretToCredentials(Secret secret) {
String namespace = secret.getMetadata().getNamespace();
String name = secret.getMetadata().getName();
String customName = secret.getMetadata().getAnnotations().get(Annotations.SECRET_NAME);

Map<String, String> data = secret.getData();

if (data == null) {
Expand All @@ -233,7 +236,7 @@ private static Credentials secretToCredentials(Secret secret) {
return null;
}

final String secretName = secretName(namespace, name);
final String secretName = secretName(namespace, name, customName);
switch (secret.getType()) {
case OPENSHIFT_SECRETS_TYPE_OPAQUE:
String usernameData = data.get(OPENSHIFT_SECRETS_DATA_USERNAME);
Expand Down

0 comments on commit 0136d1c

Please sign in to comment.