|
23 | 23 | import ch.qos.logback.core.UnsynchronizedAppenderBase; |
24 | 24 | import ch.qos.logback.core.util.Loader; |
25 | 25 | import com.google.api.core.InternalApi; |
| 26 | +import com.google.api.core.ObsoleteApi; |
26 | 27 | import com.google.auth.oauth2.GoogleCredentials; |
27 | 28 | import com.google.cloud.MonitoredResource; |
28 | 29 | import com.google.cloud.logging.Instrumentation; |
|
35 | 36 | import com.google.cloud.logging.Payload; |
36 | 37 | import com.google.cloud.logging.Severity; |
37 | 38 | import com.google.cloud.logging.Synchronicity; |
| 39 | +import com.google.common.base.Preconditions; |
38 | 40 | import com.google.common.base.Strings; |
39 | 41 | import com.google.common.collect.ImmutableList; |
40 | | -import java.io.FileInputStream; |
41 | 42 | import java.io.IOException; |
| 43 | +import java.nio.file.Files; |
| 44 | +import java.nio.file.Paths; |
42 | 45 | import java.time.Instant; |
43 | 46 | import java.util.ArrayList; |
44 | 47 | import java.util.HashMap; |
@@ -138,6 +141,7 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> { |
138 | 141 | private String log; |
139 | 142 | private String resourceType; |
140 | 143 | private String credentialsFile; |
| 144 | + private GoogleCredentials credentials; |
141 | 145 | private String logDestinationProjectId; |
142 | 146 | private boolean autoPopulateMetadata = true; |
143 | 147 | private boolean redirectToStdout = false; |
@@ -185,17 +189,46 @@ public void setResourceType(String resourceType) { |
185 | 189 | } |
186 | 190 |
|
187 | 191 | /** |
188 | | - * Sets the path to the <a |
| 192 | + * This method is obsolete because of a potential security risk. Use the {@link |
| 193 | + * #setCredentials(GoogleCredentials)} method instead. |
| 194 | + * |
| 195 | + * <p>If you know that you will be loading credential configurations of a specific type, it is |
| 196 | + * recommended to use a credential-type-specific `fromStream()` method. This will ensure that an |
| 197 | + * unexpected credential type with potential for malicious intent is not loaded unintentionally. |
| 198 | + * You might still have to do validation for certain credential types. Please follow the |
| 199 | + * recommendation for that method. |
| 200 | + * |
| 201 | + * <p>If you are loading your credential configuration from an untrusted source and have not |
| 202 | + * mitigated the risks (e.g. by validating the configuration yourself), make these changes as soon |
| 203 | + * as possible to prevent security risks to your environment. |
| 204 | + * |
| 205 | + * <p>Regardless of the method used, it is always your responsibility to validate configurations |
| 206 | + * received from external sources. |
| 207 | + * |
| 208 | + * <p>Sets the path to the <a |
189 | 209 | * href="https://cloud.google.com/iam/docs/creating-managing-service-account-keys">credential |
190 | 210 | * file</a>. If not set the appender will use {@link GoogleCredentials#getApplicationDefault()} to |
191 | 211 | * authenticate. |
192 | 212 | * |
193 | 213 | * @param credentialsFile the path to the credentials file. |
194 | 214 | */ |
| 215 | + @ObsoleteApi( |
| 216 | + "This method is obsolete because of a potential security risk. Use the setCredentials() method instead") |
195 | 217 | public void setCredentialsFile(String credentialsFile) { |
196 | 218 | this.credentialsFile = credentialsFile; |
197 | 219 | } |
198 | 220 |
|
| 221 | + /** |
| 222 | + * Sets the credential to use. If not set the appender will use {@link |
| 223 | + * GoogleCredentials#getApplicationDefault()} to authenticate. |
| 224 | + * |
| 225 | + * @param credentials the GoogleCredentials to set |
| 226 | + */ |
| 227 | + public void setCredentials(GoogleCredentials credentials) { |
| 228 | + Preconditions.checkNotNull(credentials, "Credentials cannot be null"); |
| 229 | + this.credentials = credentials; |
| 230 | + } |
| 231 | + |
199 | 232 | /** |
200 | 233 | * Sets project ID to be used to customize log destination name for written log entries. |
201 | 234 | * |
@@ -445,10 +478,12 @@ protected LoggingOptions getLoggingOptions() { |
445 | 478 | if (loggingOptions == null) { |
446 | 479 | LoggingOptions.Builder builder = LoggingOptions.newBuilder(); |
447 | 480 | builder.setProjectId(logDestinationProjectId); |
448 | | - if (!Strings.isNullOrEmpty(credentialsFile)) { |
| 481 | + if (credentials != null) { |
| 482 | + builder.setCredentials(credentials); |
| 483 | + } else if (!Strings.isNullOrEmpty(credentialsFile)) { |
449 | 484 | try { |
450 | 485 | builder.setCredentials( |
451 | | - GoogleCredentials.fromStream(new FileInputStream(credentialsFile))); |
| 486 | + GoogleCredentials.fromStream(Files.newInputStream(Paths.get(credentialsFile)))); |
452 | 487 | } catch (IOException e) { |
453 | 488 | throw new RuntimeException( |
454 | 489 | String.format( |
|
0 commit comments