-
Notifications
You must be signed in to change notification settings - Fork 148
Add Kubernetes Auth method #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions
63
src/main/java/com/datapipe/jenkins/vault/credentials/VaultKubernetesCredential.java
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,63 @@ | ||
| package com.datapipe.jenkins.vault.credentials; | ||
|
|
||
| import com.bettercloud.vault.Vault; | ||
| import com.bettercloud.vault.VaultException; | ||
| import com.cloudbees.plugins.credentials.CredentialsScope; | ||
| import com.datapipe.jenkins.vault.exception.VaultPluginException; | ||
| import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import hudson.Extension; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.stream.Collectors; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
|
|
||
| public class VaultKubernetesCredential extends AbstractVaultTokenCredential { | ||
|
|
||
| private static final String SERVICE_ACCOUNT_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token"; | ||
|
|
||
| @NonNull | ||
| private final String role; | ||
|
|
||
| @DataBoundConstructor | ||
| public VaultKubernetesCredential(@CheckForNull CredentialsScope scope, @CheckForNull String id, | ||
| @CheckForNull String description, @NonNull String role) { | ||
| super(scope, id, description); | ||
| this.role = role; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME") | ||
| public String getToken(Vault vault) { | ||
| String jwt; | ||
| try { | ||
| jwt = Files.lines(Paths.get(SERVICE_ACCOUNT_TOKEN_PATH)).collect(Collectors.joining()); | ||
| } catch (IOException e) { | ||
| throw new VaultPluginException("could not get JWT from Service Account Token", e); | ||
| } | ||
|
|
||
| try { | ||
| return vault | ||
| .withRetries(5, 500) | ||
| .auth() | ||
| .loginByKubernetes(role, jwt) | ||
| .getAuthClientToken(); | ||
| } catch (VaultException e) { | ||
| throw new VaultPluginException("could not log in into vault", e); | ||
| } | ||
| } | ||
|
|
||
| @Extension | ||
| public static class DescriptorImpl extends BaseStandardCredentialsDescriptor { | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "Vault Kubernetes Credential"; | ||
| } | ||
|
|
||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
...ources/com/datapipe/jenkins/vault/credentials/VaultKubernetesCredential/credentials.jelly
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,8 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <?jelly escape-by-default='true'?> | ||
| <j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler"> | ||
| <f:entry title="Role"> | ||
| <f:textbox field="role" name="role"/> | ||
| </f:entry> | ||
| <st:include page="id-and-description" class="${descriptor.clazz}"/> | ||
| </j:jelly> |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This path is not the same as it would be on Windows?
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.
I don't really know that. The windows container support in kubernetes is relatively new and after a quick search I couldn't find any documentation on how the volumes work in windows containers, but I guess as in any regular windows.
The service account token is just a regular secret, so technically it can be mounted on any path even in unix containers. But the Service Account Admission Controller, which manages service accounts in kubernetes, mounts the token on this path by default. link to the documentation
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.
Perhaps this option should only be visible if kubernetes secret is detected?
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 be achieved with
isApplicablein the descriptorThere 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.
Do you mean to only show the VaultKubernetesCredential as option, when the mounted token is detected?
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.
Simply a suggestion, don't know if it makes sense. I am fine with the feature as is.