|
35 | 35 |
|
36 | 36 | /** Provides a library of utility functions for credentials-related work. */ |
37 | 37 | public class CredentialsUtil { |
38 | | - /** |
39 | | - * Get the Google Robot Credentials for the given credentialsId. |
40 | | - * |
41 | | - * @param itemGroup A handle to the Jenkins instance. Must be non-null. |
42 | | - * @param domainRequirements A list of domain requirements. Must be non-null. |
43 | | - * @param credentialsId The ID of the GoogleRobotCredentials to be retrieved from Jenkins and |
44 | | - * utilized for authorization. Must be non-empty or non-null and exist in credentials store. |
45 | | - * @return Google Robot Credential for the given credentialsId. |
46 | | - * @throws AbortException If there was an issue retrieving the Google Robot Credentials. |
47 | | - */ |
48 | | - public static GoogleRobotCredentials getRobotCredentials( |
49 | | - ItemGroup itemGroup, |
50 | | - ImmutableList<DomainRequirement> domainRequirements, |
51 | | - String credentialsId) |
52 | | - throws AbortException { |
53 | | - Preconditions.checkNotNull(itemGroup); |
54 | | - Preconditions.checkNotNull(domainRequirements); |
55 | | - Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
| 38 | + /** |
| 39 | + * Get the Google Robot Credentials for the given credentialsId. |
| 40 | + * |
| 41 | + * @param itemGroup A handle to the Jenkins instance. Must be non-null. |
| 42 | + * @param domainRequirements A list of domain requirements. Must be non-null. |
| 43 | + * @param credentialsId The ID of the GoogleRobotCredentials to be retrieved from Jenkins and |
| 44 | + * utilized for authorization. Must be non-empty or non-null and exist in credentials store. |
| 45 | + * @return Google Robot Credential for the given credentialsId. |
| 46 | + * @throws AbortException If there was an issue retrieving the Google Robot Credentials. |
| 47 | + */ |
| 48 | + public static GoogleRobotCredentials getRobotCredentials( |
| 49 | + ItemGroup itemGroup, ImmutableList<DomainRequirement> domainRequirements, String credentialsId) |
| 50 | + throws AbortException { |
| 51 | + Preconditions.checkNotNull(itemGroup); |
| 52 | + Preconditions.checkNotNull(domainRequirements); |
| 53 | + Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
56 | 54 |
|
57 | | - GoogleRobotCredentials robotCreds = |
58 | | - CredentialsMatchers.firstOrNull( |
59 | | - CredentialsProvider.lookupCredentials( |
60 | | - GoogleRobotCredentials.class, itemGroup, ACL.SYSTEM, domainRequirements), |
61 | | - CredentialsMatchers.withId(credentialsId)); |
| 55 | + GoogleRobotCredentials robotCreds = CredentialsMatchers.firstOrNull( |
| 56 | + CredentialsProvider.lookupCredentials( |
| 57 | + GoogleRobotCredentials.class, itemGroup, ACL.SYSTEM, domainRequirements), |
| 58 | + CredentialsMatchers.withId(credentialsId)); |
62 | 59 |
|
63 | | - if (robotCreds == null) { |
64 | | - throw new AbortException(Messages.ClientFactory_FailedToRetrieveCredentials(credentialsId)); |
| 60 | + if (robotCreds == null) { |
| 61 | + throw new AbortException(Messages.ClientFactory_FailedToRetrieveCredentials(credentialsId)); |
| 62 | + } |
| 63 | + |
| 64 | + return robotCreds; |
65 | 65 | } |
66 | 66 |
|
67 | | - return robotCreds; |
68 | | - } |
| 67 | + /** |
| 68 | + * Get the Credential from the Google robot credentials for GKE access. |
| 69 | + * |
| 70 | + * @param robotCreds Google Robot Credential for desired service account. |
| 71 | + * @return Google Credential for the service account. |
| 72 | + * @throws AbortException if there was an error initializing HTTP transport. |
| 73 | + */ |
| 74 | + public static Credential getGoogleCredential(GoogleRobotCredentials robotCreds) throws AbortException { |
| 75 | + Credential credential; |
| 76 | + try { |
| 77 | + credential = robotCreds.getGoogleCredential(new ContainerScopeRequirement()); |
| 78 | + } catch (GeneralSecurityException gse) { |
| 79 | + throw new AbortException(Messages.ClientFactory_FailedToInitializeHTTPTransport(gse.getMessage())); |
| 80 | + } |
69 | 81 |
|
70 | | - /** |
71 | | - * Get the Credential from the Google robot credentials for GKE access. |
72 | | - * |
73 | | - * @param robotCreds Google Robot Credential for desired service account. |
74 | | - * @return Google Credential for the service account. |
75 | | - * @throws AbortException if there was an error initializing HTTP transport. |
76 | | - */ |
77 | | - public static Credential getGoogleCredential(GoogleRobotCredentials robotCreds) |
78 | | - throws AbortException { |
79 | | - Credential credential; |
80 | | - try { |
81 | | - credential = robotCreds.getGoogleCredential(new ContainerScopeRequirement()); |
82 | | - } catch (GeneralSecurityException gse) { |
83 | | - throw new AbortException( |
84 | | - Messages.ClientFactory_FailedToInitializeHTTPTransport(gse.getMessage())); |
| 82 | + return credential; |
85 | 83 | } |
86 | 84 |
|
87 | | - return credential; |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * Given a credentialsId and Jenkins context, returns the access token. |
92 | | - * |
93 | | - * @param itemGroup A handle to the Jenkins instance. Must be non-null. |
94 | | - * @param credentialsId The service account credential's id. Must be non-null. |
95 | | - * @return Access token from OAuth to allow kubectl to interact with the cluster. |
96 | | - * @throws IOException If an error occurred fetching the access token. |
97 | | - */ |
98 | | - static String getAccessToken(ItemGroup itemGroup, String credentialsId) throws IOException { |
99 | | - Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
100 | | - Preconditions.checkNotNull(itemGroup); |
101 | | - GoogleRobotCredentials robotCreds = |
102 | | - getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId); |
| 85 | + /** |
| 86 | + * Given a credentialsId and Jenkins context, returns the access token. |
| 87 | + * |
| 88 | + * @param itemGroup A handle to the Jenkins instance. Must be non-null. |
| 89 | + * @param credentialsId The service account credential's id. Must be non-null. |
| 90 | + * @return Access token from OAuth to allow kubectl to interact with the cluster. |
| 91 | + * @throws IOException If an error occurred fetching the access token. |
| 92 | + */ |
| 93 | + static String getAccessToken(ItemGroup itemGroup, String credentialsId) throws IOException { |
| 94 | + Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
| 95 | + Preconditions.checkNotNull(itemGroup); |
| 96 | + GoogleRobotCredentials robotCreds = getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId); |
103 | 97 |
|
104 | | - Credential googleCredential = getGoogleCredential(robotCreds); |
105 | | - return getAccessToken(googleCredential); |
106 | | - } |
107 | | - /** |
108 | | - * Wrapper to get access token for service account with this credentialsId. Uses Jenkins.get() as |
109 | | - * context. |
110 | | - * |
111 | | - * @param credentialsId The service account credential's id. Must be non-null. |
112 | | - * @return Access token from OAuth to allow kubectl to interact with the cluster. |
113 | | - * @throws IOException If an error occurred fetching the access token. |
114 | | - */ |
115 | | - static String getAccessToken(String credentialsId) throws IOException { |
116 | | - return getAccessToken(Jenkins.get(), credentialsId); |
117 | | - } |
| 98 | + Credential googleCredential = getGoogleCredential(robotCreds); |
| 99 | + return getAccessToken(googleCredential); |
| 100 | + } |
| 101 | + /** |
| 102 | + * Wrapper to get access token for service account with this credentialsId. Uses Jenkins.get() as |
| 103 | + * context. |
| 104 | + * |
| 105 | + * @param credentialsId The service account credential's id. Must be non-null. |
| 106 | + * @return Access token from OAuth to allow kubectl to interact with the cluster. |
| 107 | + * @throws IOException If an error occurred fetching the access token. |
| 108 | + */ |
| 109 | + static String getAccessToken(String credentialsId) throws IOException { |
| 110 | + return getAccessToken(Jenkins.get(), credentialsId); |
| 111 | + } |
118 | 112 |
|
119 | | - /** |
120 | | - * Given the Google Credential, retrieve the access token. |
121 | | - * |
122 | | - * @param googleCredential Google Credential to get an access token. Must be non-null. |
123 | | - * @return Access token from OAuth to allow kubectl to interact with the cluster. |
124 | | - * @throws IOException If an error occured fetching the access token. |
125 | | - */ |
126 | | - static String getAccessToken(Credential googleCredential) throws IOException { |
127 | | - Preconditions.checkNotNull(googleCredential); |
| 113 | + /** |
| 114 | + * Given the Google Credential, retrieve the access token. |
| 115 | + * |
| 116 | + * @param googleCredential Google Credential to get an access token. Must be non-null. |
| 117 | + * @return Access token from OAuth to allow kubectl to interact with the cluster. |
| 118 | + * @throws IOException If an error occured fetching the access token. |
| 119 | + */ |
| 120 | + static String getAccessToken(Credential googleCredential) throws IOException { |
| 121 | + Preconditions.checkNotNull(googleCredential); |
128 | 122 |
|
129 | | - googleCredential.refreshToken(); |
130 | | - return googleCredential.getAccessToken(); |
131 | | - } |
| 123 | + googleCredential.refreshToken(); |
| 124 | + return googleCredential.getAccessToken(); |
| 125 | + } |
132 | 126 |
|
133 | | - /** |
134 | | - * Given a credentialsId and Jenkins context, return the default project ID for the service |
135 | | - * account credentials specified. |
136 | | - * |
137 | | - * @param itemGroup A handle to the Jenkins instance. Must be non-null. |
138 | | - * @param credentialsId The service account credential's ID. Must be non-null. |
139 | | - * @return The project ID specified when creating the credential in the Jenkins credential store. |
140 | | - * @throws AbortException If an error occurred fetching the credential. |
141 | | - */ |
142 | | - static String getDefaultProjectId(ItemGroup itemGroup, String credentialsId) |
143 | | - throws AbortException { |
144 | | - Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
145 | | - Preconditions.checkNotNull(itemGroup); |
146 | | - GoogleRobotCredentials robotCreds = |
147 | | - getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId); |
148 | | - return Strings.isNullOrEmpty(robotCreds.getProjectId()) ? "" : robotCreds.getProjectId(); |
149 | | - } |
| 127 | + /** |
| 128 | + * Given a credentialsId and Jenkins context, return the default project ID for the service |
| 129 | + * account credentials specified. |
| 130 | + * |
| 131 | + * @param itemGroup A handle to the Jenkins instance. Must be non-null. |
| 132 | + * @param credentialsId The service account credential's ID. Must be non-null. |
| 133 | + * @return The project ID specified when creating the credential in the Jenkins credential store. |
| 134 | + * @throws AbortException If an error occurred fetching the credential. |
| 135 | + */ |
| 136 | + static String getDefaultProjectId(ItemGroup itemGroup, String credentialsId) throws AbortException { |
| 137 | + Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
| 138 | + Preconditions.checkNotNull(itemGroup); |
| 139 | + GoogleRobotCredentials robotCreds = getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId); |
| 140 | + return Strings.isNullOrEmpty(robotCreds.getProjectId()) ? "" : robotCreds.getProjectId(); |
| 141 | + } |
150 | 142 |
|
151 | | - /** |
152 | | - * Wrapper to get the default project ID for a credential using Jenkins.get() as the context. |
153 | | - * |
154 | | - * @param credentialsId The service account credential's ID. Must be non-null. |
155 | | - * @return The project ID specified when creating the credential in the Jenkins credential store. |
156 | | - * @throws AbortException If an error occurred fetching the credential. |
157 | | - */ |
158 | | - static String getDefaultProjectId(String credentialsId) throws AbortException { |
159 | | - Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
160 | | - return getDefaultProjectId(Jenkins.get(), credentialsId); |
161 | | - } |
| 143 | + /** |
| 144 | + * Wrapper to get the default project ID for a credential using Jenkins.get() as the context. |
| 145 | + * |
| 146 | + * @param credentialsId The service account credential's ID. Must be non-null. |
| 147 | + * @return The project ID specified when creating the credential in the Jenkins credential store. |
| 148 | + * @throws AbortException If an error occurred fetching the credential. |
| 149 | + */ |
| 150 | + static String getDefaultProjectId(String credentialsId) throws AbortException { |
| 151 | + Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId)); |
| 152 | + return getDefaultProjectId(Jenkins.get(), credentialsId); |
| 153 | + } |
162 | 154 | } |
0 commit comments