Skip to content
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

NIFI-13231 Added Private key authentication for GitHubFlowRegistryClient #8890

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,11 @@
<artifactId>github-api</artifactId>
<version>${github-api.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.12.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum GitHubAuthenticationType {

NONE,
PERSONAL_ACCESS_TOKEN,
APP_INSTALLATION_TOKEN;
APP_INSTALLATION_TOKEN,
APP_INSTALLATION_ID_AND_PRIVATE_KEY;

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,22 @@ public class GitHubFlowRegistryClient extends AbstractFlowRegistryClient {
.sensitive(true)
.dependsOn(AUTHENTICATION_TYPE, GitHubAuthenticationType.APP_INSTALLATION_TOKEN.name())
.build();

static final PropertyDescriptor PRIVATE_KEY = new PropertyDescriptor.Builder()
.name("Private Key")
.description("RSA private key associated with GitHub App to use for authentication.")
.addValidator(StandardValidators.NON_BLANK_VALIDATOR)
.required(true)
.sensitive(true)
.dependsOn(AUTHENTICATION_TYPE, GitHubAuthenticationType.APP_INSTALLATION_ID_AND_PRIVATE_KEY.name())
.build();
static final PropertyDescriptor APP_ID = new PropertyDescriptor.Builder()
.name("App ID")
.description("Identifier of GitHub App to use for authentication")
.addValidator(StandardValidators.NON_BLANK_VALIDATOR)
.required(true)
.sensitive(false)
.dependsOn(AUTHENTICATION_TYPE, GitHubAuthenticationType.APP_INSTALLATION_ID_AND_PRIVATE_KEY.name())
.build();
static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = List.of(
GITHUB_API_URL,
REPOSITORY_OWNER,
Expand All @@ -137,8 +152,9 @@ public class GitHubFlowRegistryClient extends AbstractFlowRegistryClient {
REPOSITORY_PATH,
AUTHENTICATION_TYPE,
PERSONAL_ACCESS_TOKEN,
APP_INSTALLATION_TOKEN
);
APP_INSTALLATION_TOKEN,
PRIVATE_KEY,
APP_ID);

static final String DEFAULT_BUCKET_NAME = "default";
static final String DEFAULT_BUCKET_KEEP_FILE_PATH = DEFAULT_BUCKET_NAME + "/.keep";
Expand Down Expand Up @@ -641,6 +657,8 @@ protected GitHubRepositoryClient createRepositoryClient(final FlowRegistryClient
.authenticationType(GitHubAuthenticationType.valueOf(context.getProperty(AUTHENTICATION_TYPE).getValue()))
.personalAccessToken(context.getProperty(PERSONAL_ACCESS_TOKEN).getValue())
.appInstallationToken(context.getProperty(APP_INSTALLATION_TOKEN).getValue())
.appId(context.getProperty(APP_ID).getValue())
.privateKey(context.getProperty(PRIVATE_KEY).getValue())
.repoOwner(context.getProperty(REPOSITORY_OWNER).getValue())
.repoName(context.getProperty(REPOSITORY_NAME).getValue())
.repoPath(context.getProperty(REPOSITORY_PATH).getValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

package org.apache.nifi.github;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.nifi.registry.flow.FlowRegistryException;
import org.bouncycastle.asn1.pkcs.RSAPrivateKey;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
exceptionfactory marked this conversation as resolved.
Show resolved Hide resolved
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHContentUpdateResponse;
Expand All @@ -29,16 +34,18 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.extras.authorization.JWTTokenProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.io.StringReader;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;
exceptionfactory marked this conversation as resolved.
Show resolved Hide resolved
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -74,6 +81,15 @@ private GitHubRepositoryClient(final Builder builder) throws IOException, FlowRe
switch (authenticationType) {
case PERSONAL_ACCESS_TOKEN -> gitHubBuilder.withOAuthToken(builder.personalAccessToken);
case APP_INSTALLATION_TOKEN -> gitHubBuilder.withAppInstallationToken(builder.appInstallationToken);
case APP_INSTALLATION_ID_AND_PRIVATE_KEY -> {
try {
JWTTokenProvider jwtTokenProvider = new JWTTokenProvider(builder().appId, builder().privateKey);
String token = jwtTokenProvider.getEncodedAuthorization();
gitHubBuilder.withJwtToken(token);
} catch (Exception e) {
throw new FlowRegistryException(e.getMessage());
exceptionfactory marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

gitHub = gitHubBuilder.build();
Expand Down Expand Up @@ -427,6 +443,8 @@ public static class Builder {
private String repoOwner;
private String repoName;
private String repoPath;
private String privateKey;
private String appId;

public Builder apiUrl(final String apiUrl) {
this.apiUrl = apiUrl;
Expand Down Expand Up @@ -462,6 +480,15 @@ public Builder repoPath(final String repoPath) {
this.repoPath = repoPath;
return this;
}
public Builder appId(final String appId){
this.appId = appId;
return this;
}

public Builder privateKey(final String privateKey){
this.privateKey = privateKey;
return this;
}

public GitHubRepositoryClient build() throws IOException, FlowRegistryException {
return new GitHubRepositoryClient(this);
Expand Down