Skip to content

Commit 1fb97c6

Browse files
Updated code
1 parent eaf7cda commit 1fb97c6

File tree

1 file changed

+44
-13
lines changed
  • dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils

1 file changed

+44
-13
lines changed

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/GithubAppCheck.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,55 @@ public GithubAppCheck(final Namespace ns){
3636
this.privateKeyPath = ns.get(Constants.SKIP_GITHUB_APP_KEY);
3737
this.jwt = null;
3838
this.jwtExpiry = null;
39-
try {
40-
generateJWT(this.appId, this.privateKeyPath);
41-
} catch (GeneralSecurityException | IOException exception) {
42-
log.warn("Could not initialise JWT due to exception: {}", exception.getMessage());
39+
this.gitHub = null;
40+
if (this.appId != null && this.privateKeyPath != null) {
41+
try {
42+
generateJWT(this.appId, this.privateKeyPath);
43+
} catch (GeneralSecurityException | IOException exception) {
44+
log.warn("Could not initialise JWT due to exception: {}", exception.getMessage());
45+
}
46+
try {
47+
this.gitHub = new GitHubBuilder()
48+
.withEndpoint(CommandLine.gitApiUrl(ns))
49+
.withJwtToken(jwt)
50+
.build();
51+
} catch (IOException exception) {
52+
log.warn("Could not initialise github due to exception: {}", exception.getMessage());
53+
}
4354
}
44-
try {
45-
this.gitHub = new GitHubBuilder()
46-
.withEndpoint(CommandLine.gitApiUrl(ns))
47-
.withJwtToken(jwt)
48-
.build();
49-
} catch (IOException exception) {
50-
log.warn("Could not initialise github due to exception: {}", exception.getMessage());
51-
this.gitHub = null;
55+
else {
56+
log.warn("Could not find any Github app ID and Github app Key in the declared list. Hence assuming this class is no longer needed");
5257
}
5358
}
5459

60+
/**
61+
* Method to verify whether the github app is installed on a repository or not.
62+
* @param fullRepoName = The repository full name, i.e, of the format "owner/repoName". Eg: "Salesforce/dockerfile-image-update"
63+
* @return True if github app is installed, false otherwise.
64+
*/
5565
protected boolean isGithubAppEnabledOnRepository(String fullRepoName){
5666
refreshJwtIfNeeded(appId, privateKeyPath);
5767
try {
5868
gitHub.getApp().getInstallationByRepository(fullRepoName.split("/")[0], fullRepoName.split("/")[1]);
5969
return true;
6070
} catch (HttpException exception) {
6171
if (exception.getResponseCode() != 404) {
72+
// Log for any HTTP status code other than 404 Not found.
6273
log.warn("Caught a HTTPException {} while trying to get app installation. Defaulting to False", exception.getMessage());
6374
}
6475
return false;
6576
} catch (IOException exception) {
77+
// Most often happens on timeout scenarios.
6678
log.warn("Caught a IOException {} while trying to get app installation. Defaulting to False", exception.getMessage());
6779
return false;
6880
}
6981
}
7082

83+
/**
84+
* Method to refresh the JWT token if needed. Checks the JWT expiry time, and if it is 60s away from expiring, refreshes it.
85+
* @param appId = The id of the Github App to generate the JWT for
86+
* @param privateKeyPath = The path to the private key of the Github App to generate the JWT for
87+
*/
7188
private void refreshJwtIfNeeded(String appId, String privateKeyPath){
7289
if (jwt == null || jwtExpiry.isBefore(Instant.now().minusSeconds(60))) { // Adding a buffer to ensure token validity
7390
try {
@@ -78,6 +95,14 @@ private void refreshJwtIfNeeded(String appId, String privateKeyPath){
7895
}
7996
}
8097

98+
/**
99+
* Method to generate the JWT used to access the Github App APIs. We generate the JWT to be valid for 600 seconds.
100+
* Along with the JWT value, the jwtExpiry value is set to the time of 600 sec from now.
101+
* @param appId = The id of the Github App to generate the JWT for
102+
* @param privateKeyPath = The path to the private key of the Github App to generate the JWT for
103+
* @throws IOException
104+
* @throws GeneralSecurityException
105+
*/
81106
private void generateJWT(String appId, String privateKeyPath) throws IOException, GeneralSecurityException {
82107
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
83108
RSAPrivateKey privateKey = getRSAPrivateKey(privateKeyPath);
@@ -92,12 +117,18 @@ private void generateJWT(String appId, String privateKeyPath) throws IOException
92117
jwtExpiry = now.plusSeconds(600);
93118
}
94119

120+
/**
121+
* The method to get the private key in an RSA Encoded format. Makes use of org.bouncycastle.util
122+
* @param privateKeyPath
123+
* @return
124+
* @throws IOException
125+
* @throws GeneralSecurityException
126+
*/
95127
private RSAPrivateKey getRSAPrivateKey(String privateKeyPath) throws IOException, GeneralSecurityException {
96128
try (PemReader pemReader = new PemReader(new FileReader(new File(privateKeyPath)))) {
97129
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pemReader.readPemObject().getContent());
98130
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
99131
return (RSAPrivateKey) keyFactory.generatePrivate(spec);
100132
}
101133
}
102-
103134
}

0 commit comments

Comments
 (0)