| 138 | env_(env) {} |
| 139 | |
| 140 | Status GoogleAuthProvider::GetToken(string* t) { |
| 141 | mutex_lock lock(mu_); |
| 142 | const uint64 now_sec = env_->NowSeconds(); |
| 143 | |
| 144 | if (now_sec + kExpirationTimeMarginSec < expiration_timestamp_sec_) { |
| 145 | *t = current_token_; |
| 146 | return Status::OK(); |
| 147 | } |
| 148 | |
| 149 | if (GetTokenForTesting().ok()) { |
| 150 | *t = current_token_; |
| 151 | return Status::OK(); |
| 152 | } |
| 153 | |
| 154 | auto token_from_files_status = GetTokenFromFiles(); |
| 155 | if (token_from_files_status.ok()) { |
| 156 | *t = current_token_; |
| 157 | return Status::OK(); |
| 158 | } |
| 159 | |
| 160 | char* no_gce_check_var = std::getenv(kNoGceCheck); |
| 161 | bool skip_gce_check = no_gce_check_var != nullptr && |
| 162 | absl::EqualsIgnoreCase(no_gce_check_var, "true"); |
| 163 | Status token_from_gce_status; |
| 164 | if (skip_gce_check) { |
| 165 | token_from_gce_status = |
| 166 | Status(error::CANCELLED, |
| 167 | strings::StrCat("GCE check skipped due to presence of $", |
| 168 | kNoGceCheck, " environment variable.")); |
| 169 | } else { |
| 170 | token_from_gce_status = GetTokenFromGce(); |
| 171 | } |
| 172 | |
| 173 | if (token_from_gce_status.ok()) { |
| 174 | *t = current_token_; |
| 175 | return Status::OK(); |
| 176 | } |
| 177 | |
| 178 | LOG(WARNING) |
| 179 | << "All attempts to get a Google authentication bearer token failed, " |
| 180 | << "returning an empty token. Retrieving token from files failed with \"" |
| 181 | << token_from_files_status.ToString() << "\"." |
| 182 | << " Retrieving token from GCE failed with \"" |
| 183 | << token_from_gce_status.ToString() << "\"."; |
| 184 | |
| 185 | // Public objects can still be accessed with an empty bearer token, |
| 186 | // so return an empty token instead of failing. |
| 187 | *t = ""; |
| 188 | |
| 189 | // We only want to keep returning our empty token if we've tried and failed |
| 190 | // the (potentially slow) task of detecting GCE. |
| 191 | if (skip_gce_check) { |
| 192 | expiration_timestamp_sec_ = 0; |
| 193 | } else { |
| 194 | expiration_timestamp_sec_ = UINT64_MAX; |
| 195 | } |
| 196 | current_token_ = ""; |
| 197 | |