-
Notifications
You must be signed in to change notification settings - Fork 751
Introduce extensible configuration fetcher SPI for JCasC #2865
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
Draft
somiljain2006
wants to merge
13
commits into
jenkinsci:master
Choose a base branch
from
somiljain2006:Private-repo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
638e2d2
Introduce extensible configuration fetcher SPI for JCasC
somiljain2006 10c5aae
Remove unused line
somiljain2006 cbce44d
Fix test failures
somiljain2006 b16138b
Fix checkstyle errors
somiljain2006 3f2ab52
Fix spotbugs errors
somiljain2006 687818d
Added tests
somiljain2006 4865c3e
Clean the code
somiljain2006 3ca5f24
Fixed regression
somiljain2006 2133619
Change error message
somiljain2006 176c18f
Remove flaky test
somiljain2006 d04d196
Added defense handling and change URLConnection
somiljain2006 367bb33
chore: trigger CI pipeline
somiljain2006 bb639a9
chore: trigger CI pipeline
somiljain2006 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
Some comments aren't visible on the classic Files Changed page.
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
46 changes: 46 additions & 0 deletions
46
plugin/src/main/java/io/jenkins/plugins/casc/fetcher/BootstrapEnvVarCredentialResolver.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,46 @@ | ||
| package io.jenkins.plugins.casc.fetcher; | ||
|
|
||
| public final class BootstrapEnvVarCredentialResolver { | ||
|
|
||
| public static final BootstrapEnvVarCredentialResolver INSTANCE = new BootstrapEnvVarCredentialResolver(); | ||
|
|
||
| private BootstrapEnvVarCredentialResolver() {} | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public <T extends FetchAuthData> T resolve(String credentialId, Class<T> type) { | ||
| if (credentialId == null || credentialId.isEmpty() || type == null) { | ||
| return null; | ||
| } | ||
| if (type == FetchAuthData.Token.class) { | ||
| String token = System.getenv(credentialId); | ||
| if (token != null && !token.isEmpty()) { | ||
| return (T) (FetchAuthData.Token) () -> token; | ||
| } | ||
| } | ||
|
|
||
| if (type == FetchAuthData.SshKey.class) { | ||
| String privateKey = System.getenv(credentialId + "_PRIVATE_KEY"); | ||
| if (privateKey != null && !privateKey.isEmpty()) { | ||
| String username = System.getenv(credentialId + "_USERNAME"); | ||
| String passphrase = System.getenv(credentialId + "_PASSPHRASE"); | ||
| return (T) new FetchAuthData.SshKey() { | ||
| @Override | ||
| public String getUsername() { | ||
| return username != null ? username : "git"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getPrivateKey() { | ||
| return privateKey; | ||
| } | ||
|
|
||
| @Override | ||
| public String getPassphrase() { | ||
| return passphrase; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
plugin/src/main/java/io/jenkins/plugins/casc/fetcher/CasCConfigFetcher.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,11 @@ | ||
| package io.jenkins.plugins.casc.fetcher; | ||
|
|
||
| import hudson.ExtensionPoint; | ||
| import java.io.IOException; | ||
|
|
||
| public interface CasCConfigFetcher extends ExtensionPoint { | ||
|
|
||
| boolean supports(String location); | ||
|
|
||
| FetchResult fetch(String location, FetchCredentials credentials) throws IOException; | ||
| } |
67 changes: 67 additions & 0 deletions
67
plugin/src/main/java/io/jenkins/plugins/casc/fetcher/DefaultHttpFetcher.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,67 @@ | ||
| package io.jenkins.plugins.casc.fetcher; | ||
|
|
||
| import static java.lang.Thread.currentThread; | ||
|
|
||
| import hudson.Extension; | ||
| import hudson.ProxyConfiguration; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
|
|
||
| @Extension(ordinal = -100) | ||
| public class DefaultHttpFetcher implements CasCConfigFetcher { | ||
|
|
||
| @Override | ||
| public boolean supports(String location) { | ||
| return location != null && (location.startsWith("http://") || location.startsWith("https://")); | ||
| } | ||
|
|
||
| @Override | ||
| public FetchResult fetch(String location, FetchCredentials credentials) throws IOException { | ||
| URI uri; | ||
| try { | ||
|
somiljain2006 marked this conversation as resolved.
|
||
| uri = new URI(location); | ||
| } catch (URISyntaxException e) { | ||
| throw new IOException("Invalid URL: " + location, e); | ||
| } | ||
|
|
||
| String path = uri.getPath(); | ||
| String fileName = | ||
| (path != null && path.contains("/")) ? path.substring(path.lastIndexOf('/') + 1) : "casc.yaml"; | ||
|
|
||
| if (fileName.isEmpty()) { | ||
| fileName = "casc.yaml"; | ||
| } | ||
|
|
||
| HttpClient client = ProxyConfiguration.newHttpClient(); | ||
|
|
||
| HttpRequest request = ProxyConfiguration.newHttpRequestBuilder(uri) | ||
| .GET() | ||
| .timeout(Duration.ofSeconds(30)) | ||
| .build(); | ||
|
|
||
| byte[] yamlBytes; | ||
| try { | ||
| HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); | ||
|
|
||
| if (response.statusCode() < 200 || response.statusCode() >= 300) { | ||
| throw new IOException("Failed to fetch configuration from " + location + ". HTTP status code: " | ||
| + response.statusCode()); | ||
| } | ||
|
|
||
| yamlBytes = response.body(); | ||
| } catch (InterruptedException e) { | ||
| currentThread().interrupt(); | ||
| throw new IOException("Interrupted while fetching configuration from: " + location, e); | ||
| } | ||
|
|
||
| ResolvedYaml resolved = new ResolvedYaml(fileName, () -> new java.io.ByteArrayInputStream(yamlBytes)); | ||
|
|
||
| return new FetchResult(Collections.singletonList(resolved), (AutoCloseable) null); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
plugin/src/main/java/io/jenkins/plugins/casc/fetcher/EnvVarFetchCredentialsProvider.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,12 @@ | ||
| package io.jenkins.plugins.casc.fetcher; | ||
|
|
||
| import hudson.Extension; | ||
|
|
||
| @Extension(ordinal = -100) | ||
| public class EnvVarFetchCredentialsProvider implements FetchCredentialsProvider { | ||
|
|
||
| @Override | ||
| public <T extends FetchAuthData> T getCredentials(String credentialId, Class<T> type) { | ||
| return BootstrapEnvVarCredentialResolver.INSTANCE.resolve(credentialId, type); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
plugin/src/main/java/io/jenkins/plugins/casc/fetcher/FetchAuthData.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,25 @@ | ||
| package io.jenkins.plugins.casc.fetcher; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
|
|
||
| public interface FetchAuthData { | ||
|
|
||
| interface Token extends FetchAuthData { | ||
| String getToken(); | ||
| } | ||
|
|
||
| interface UsernamePassword extends FetchAuthData { | ||
| String getUsername(); | ||
|
|
||
| String getPassword(); | ||
| } | ||
|
|
||
| interface SshKey extends FetchAuthData { | ||
| String getUsername(); | ||
|
|
||
| String getPrivateKey(); | ||
|
|
||
| @CheckForNull | ||
| String getPassphrase(); | ||
| } | ||
| } |
Oops, something went wrong.
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.