Skip to content

Commit

Permalink
do not set WI env vars when NSA is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
itsankit-google committed Nov 8, 2023
1 parent 9bd05f5 commit 0f44398
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public void run() {
String.format("%s:%s", localhost,
cConf.getInt(Constants.ArtifactLocalizer.PORT))
));
twillPreparer = ((SecureTwillPreparer) twillPreparer)
.withNamespacedWorkloadIdentity(PreviewRunnerTwillRunnable.class.getSimpleName());
}

String priorityClass = cConf.get(Constants.Preview.CONTAINER_PRIORITY_CLASS_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public void run() {
String.format("%s:%s", localhost,
cConf.getInt(Constants.ArtifactLocalizer.PORT))
));
twillPreparer = ((SecureTwillPreparer) twillPreparer)
.withNamespacedWorkloadIdentity(TaskWorkerTwillRunnable.class.getSimpleName());
}

String priorityClass = cConf.get(Constants.TaskWorker.CONTAINER_PRIORITY_CLASS_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class KubeTwillPreparer implements DependentTwillPreparer, StatefulTwillPreparer
private final String resourcePrefix;
private final Map<String, String> extraLabels;
private final Map<String, SecretDiskRunnable> secretDiskRunnables;
private final Set<String> withNamespaceWorkloadIdentityRunnables;
private final Map<String, V1SecurityContext> containerSecurityContexts;
private final Map<String, Set<String>> readonlyDisks;
private final Map<String, Map<String, String>> runnableConfigs;
Expand Down Expand Up @@ -240,6 +241,7 @@ class KubeTwillPreparer implements DependentTwillPreparer, StatefulTwillPreparer
this.dependentRunnableNames = new HashSet<>();
this.serviceAccountName = null;
this.secretDiskRunnables = new HashMap<>();
this.withNamespaceWorkloadIdentityRunnables = new HashSet<>();
this.containerSecurityContexts = new HashMap<>();
this.readonlyDisks = new HashMap<>();
this.runnableConfigs = runnables.stream()
Expand Down Expand Up @@ -368,6 +370,12 @@ public SecureTwillPreparer withSecretDisk(String runnableName, SecretDisk... sec
return this;
}

@Override
public SecureTwillPreparer withNamespacedWorkloadIdentity(String runnableName) {
withNamespaceWorkloadIdentityRunnables.add(runnableName);
return this;
}

@Override
public SecureTwillPreparer withSecurityContext(String runnableName,
SecurityContext securityContext) {
Expand Down Expand Up @@ -1285,9 +1293,8 @@ private List<V1Container> createContainers(Map<String, RuntimeSpecification> run
environs.put(JAVA_OPTS_KEY, jvmOpts);
// Add workload identity environment variable if applicable.
if (workloadIdentityEnabled && WorkloadIdentityUtil.shouldMountWorkloadIdentity(
cdapInstallNamespace,
programRuntimeNamespace,
workloadIdentityServiceAccount)) {
cdapInstallNamespace, programRuntimeNamespace, workloadIdentityServiceAccount)
&& !withNamespaceWorkloadIdentityRunnables.contains(runnableName)) {
V1EnvVar workloadIdentityEnvVar = WorkloadIdentityUtil.generateWorkloadIdentityEnvVar();
environs.put(workloadIdentityEnvVar.getName(), workloadIdentityEnvVar.getValue());
}
Expand All @@ -1314,6 +1321,13 @@ private List<V1Container> createContainers(Map<String, RuntimeSpecification> run
.filter(entry -> !entry.getKey().equals(GCE_METADATA_HOST_ENV_VAR))
.collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue));
// Add workload identity environment variable in the dependent runnable if applicable.
if (workloadIdentityEnabled && WorkloadIdentityUtil.shouldMountWorkloadIdentity(
cdapInstallNamespace, programRuntimeNamespace, workloadIdentityServiceAccount)
&& !withNamespaceWorkloadIdentityRunnables.contains(name)) {
V1EnvVar workloadIdentityEnvVar = WorkloadIdentityUtil.generateWorkloadIdentityEnvVar();
envs.put(workloadIdentityEnvVar.getName(), workloadIdentityEnvVar.getValue());
}
mounts = addSecreteVolMountIfNeeded(spec, volumeMounts);
containers.add(
createContainer(name, podInfo.getContainerImage(), podInfo.getImagePullPolicy(), workDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public interface SecureTwillPreparer extends TwillPreparer {
SecureTwillPreparer withSecurityContext(String runnableName,
SecurityContext securityContext);

/**
* Runs the given runnable with namespace workload identity,
* this feature removes the GOOGLE_APPLICATION_CREDENTIALS environment variable
* to enable namespaced service accounts.
*
* @param runnableName name of the {@link TwillRunnable}
* @return this {@link TwillPreparer}
*/
SecureTwillPreparer withNamespacedWorkloadIdentity(String runnableName);
}

0 comments on commit 0f44398

Please sign in to comment.