|
14 | 14 |
|
15 | 15 | package com.google.devtools.build.lib.authandtls.credentialhelper; |
16 | 16 |
|
| 17 | +import static com.google.devtools.build.lib.profiler.ProfilerTask.CREDENTIAL_HELPER; |
17 | 18 | import static java.nio.charset.StandardCharsets.UTF_8; |
18 | 19 |
|
19 | 20 | import com.google.common.annotations.VisibleForTesting; |
20 | 21 | import com.google.common.base.Preconditions; |
21 | 22 | import com.google.common.collect.ImmutableList; |
22 | 23 | import com.google.common.io.CharStreams; |
| 24 | +import com.google.devtools.build.lib.profiler.Profiler; |
| 25 | +import com.google.devtools.build.lib.profiler.SilentCloseable; |
23 | 26 | import com.google.devtools.build.lib.shell.Subprocess; |
24 | 27 | import com.google.devtools.build.lib.shell.SubprocessBuilder; |
25 | 28 | import com.google.devtools.build.lib.vfs.Path; |
@@ -68,57 +71,62 @@ public GetCredentialsResponse getCredentials(CredentialHelperEnvironment environ |
68 | 71 | Preconditions.checkNotNull(environment); |
69 | 72 | Preconditions.checkNotNull(uri); |
70 | 73 |
|
71 | | - Subprocess process = spawnSubprocess(environment, "get"); |
72 | | - try (Reader stdout = new InputStreamReader(process.getInputStream(), UTF_8); |
73 | | - Reader stderr = new InputStreamReader(process.getErrorStream(), UTF_8)) { |
74 | | - try (Writer stdin = new OutputStreamWriter(process.getOutputStream(), UTF_8)) { |
75 | | - GSON.toJson(GetCredentialsRequest.newBuilder().setUri(uri).build(), stdin); |
76 | | - } |
| 74 | + Profiler prof = Profiler.instance(); |
77 | 75 |
|
78 | | - process.waitFor(); |
79 | | - if (process.timedout()) { |
80 | | - throw new CredentialHelperException( |
81 | | - String.format( |
82 | | - Locale.US, |
83 | | - "Failed to get credentials for '%s' from helper '%s': process timed out", |
84 | | - uri, |
85 | | - path)); |
86 | | - } |
87 | | - if (process.exitValue() != 0) { |
88 | | - throw new CredentialHelperException( |
89 | | - String.format( |
90 | | - Locale.US, |
91 | | - "Failed to get credentials for '%s' from helper '%s': process exited with code %d." |
92 | | - + " stderr: %s", |
93 | | - uri, |
94 | | - path, |
95 | | - process.exitValue(), |
96 | | - CharStreams.toString(stderr))); |
97 | | - } |
| 76 | + try (SilentCloseable c = prof.profile(CREDENTIAL_HELPER, "calling credential helper")) { |
| 77 | + Subprocess process = spawnSubprocess(environment, "get"); |
| 78 | + try (Reader stdout = new InputStreamReader(process.getInputStream(), UTF_8); |
| 79 | + Reader stderr = new InputStreamReader(process.getErrorStream(), UTF_8)) { |
| 80 | + try (Writer stdin = new OutputStreamWriter(process.getOutputStream(), UTF_8)) { |
| 81 | + GSON.toJson(GetCredentialsRequest.newBuilder().setUri(uri).build(), stdin); |
| 82 | + } |
| 83 | + |
| 84 | + process.waitFor(); |
98 | 85 |
|
99 | | - try { |
100 | | - GetCredentialsResponse response = GSON.fromJson(stdout, GetCredentialsResponse.class); |
101 | | - if (response == null) { |
| 86 | + if (process.timedout()) { |
102 | 87 | throw new CredentialHelperException( |
103 | 88 | String.format( |
104 | 89 | Locale.US, |
105 | | - "Failed to get credentials for '%s' from helper '%s': process exited without" |
106 | | - + " output. stderr: %s", |
| 90 | + "Failed to get credentials for '%s' from helper '%s': process timed out", |
| 91 | + uri, |
| 92 | + path)); |
| 93 | + } |
| 94 | + if (process.exitValue() != 0) { |
| 95 | + throw new CredentialHelperException( |
| 96 | + String.format( |
| 97 | + Locale.US, |
| 98 | + "Failed to get credentials for '%s' from helper '%s': process exited with code" |
| 99 | + + " %d. stderr: %s", |
107 | 100 | uri, |
108 | 101 | path, |
| 102 | + process.exitValue(), |
109 | 103 | CharStreams.toString(stderr))); |
110 | 104 | } |
111 | | - return response; |
112 | | - } catch (JsonSyntaxException e) { |
113 | | - throw new CredentialHelperException( |
114 | | - String.format( |
115 | | - Locale.US, |
116 | | - "Failed to get credentials for '%s' from helper '%s': error parsing output. stderr:" |
117 | | - + " %s", |
118 | | - uri, |
119 | | - path, |
120 | | - CharStreams.toString(stderr)), |
121 | | - e); |
| 105 | + |
| 106 | + try { |
| 107 | + GetCredentialsResponse response = GSON.fromJson(stdout, GetCredentialsResponse.class); |
| 108 | + if (response == null) { |
| 109 | + throw new CredentialHelperException( |
| 110 | + String.format( |
| 111 | + Locale.US, |
| 112 | + "Failed to get credentials for '%s' from helper '%s': process exited without" |
| 113 | + + " output. stderr: %s", |
| 114 | + uri, |
| 115 | + path, |
| 116 | + CharStreams.toString(stderr))); |
| 117 | + } |
| 118 | + return response; |
| 119 | + } catch (JsonSyntaxException e) { |
| 120 | + throw new CredentialHelperException( |
| 121 | + String.format( |
| 122 | + Locale.US, |
| 123 | + "Failed to get credentials for '%s' from helper '%s': error parsing output." |
| 124 | + + " stderr: %s", |
| 125 | + uri, |
| 126 | + path, |
| 127 | + CharStreams.toString(stderr)), |
| 128 | + e); |
| 129 | + } |
122 | 130 | } |
123 | 131 | } |
124 | 132 | } |
|
0 commit comments