|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:io'; |
| 16 | + |
| 17 | +import 'package:http/http.dart' as http; |
| 18 | + |
| 19 | +import 'bad_configuration_exception.dart'; |
| 20 | + |
| 21 | +enum MetadataValue { |
| 22 | + /// A set of typical environment variables that are likely to represent the |
| 23 | + /// current Google Cloud instance region. |
| 24 | + /// |
| 25 | + /// For context, see: |
| 26 | + /// * https://cloud.google.com/functions/docs/env-var |
| 27 | + /// * https://cloud.google.com/compute/docs/gcloud-compute#default_project |
| 28 | + /// * https://github.com/GoogleContainerTools/gcp-auth-webhook/blob/08136ca171fe5713cc70ef822c911fbd3a1707f5/server.go#L38-L44 |
| 29 | + /// |
| 30 | + /// Note: these are ordered starting from the most current/canonical to least. |
| 31 | + /// (At least as could be determined at the time of writing.) |
| 32 | + project( |
| 33 | + path: 'project/project-id', |
| 34 | + environmentValues: { |
| 35 | + 'GCP_PROJECT', |
| 36 | + 'GCLOUD_PROJECT', |
| 37 | + 'CLOUDSDK_CORE_PROJECT', |
| 38 | + 'GOOGLE_CLOUD_PROJECT', |
| 39 | + }, |
| 40 | + ), |
| 41 | + |
| 42 | + /// A set of typical environment variables that are likely to represent the |
| 43 | + /// current Google Cloud instance region. |
| 44 | + /// |
| 45 | + /// For context, see: |
| 46 | + /// * https://cloud.google.com/functions/docs/env-var |
| 47 | + /// * https://cloud.google.com/compute/docs/gcloud-compute#default_project |
| 48 | + /// * https://github.com/GoogleContainerTools/gcp-auth-webhook/blob/08136ca171fe5713cc70ef822c911fbd3a1707f5/server.go#L38-L44 |
| 49 | + /// |
| 50 | + /// Note: these are ordered starting from the most current/canonical to least. |
| 51 | + /// (At least as could be determined at the time of writing.) |
| 52 | + region( |
| 53 | + path: 'instance/region', |
| 54 | + environmentValues: { |
| 55 | + 'FUNCTION_REGION', |
| 56 | + 'CLOUDSDK_COMPUTE_REGION', |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + const MetadataValue({ |
| 61 | + required this.path, |
| 62 | + required this.environmentValues, |
| 63 | + }); |
| 64 | + |
| 65 | + final String path; |
| 66 | + |
| 67 | + final Set<String> environmentValues; |
| 68 | + |
| 69 | + /// A convenience wrapper that first tries [fromEnvironment] |
| 70 | + /// then (if the value is `null`) tries [fromMetadataServer] |
| 71 | + /// |
| 72 | + /// Like [fromMetadataServer], if no value is found, a |
| 73 | + /// [BadConfigurationException] is thrown. |
| 74 | + Future<String> fromEnvironmentOrMetadata() async { |
| 75 | + final localValue = fromEnvironment(); |
| 76 | + if (localValue != null) { |
| 77 | + return localValue; |
| 78 | + } |
| 79 | + final result = await fromMetadataServer(); |
| 80 | + |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + /// Returns the |
| 85 | + /// [Region](https://cloud.google.com/compute/docs/regions-zones#identifying_a_region_or_zone) |
| 86 | + /// for the current instance by checking the environment variables in |
| 87 | + /// [environmentValues]. |
| 88 | + /// |
| 89 | + /// The list is checked in order. This is useful for local development. |
| 90 | + /// |
| 91 | + /// If no matching variable is found, `null` is returned. |
| 92 | + String? fromEnvironment() { |
| 93 | + for (var envKey in environmentValues) { |
| 94 | + final value = Platform.environment[envKey]; |
| 95 | + if (value != null) return value; |
| 96 | + } |
| 97 | + |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + /// Returns a [Future] that completes with the |
| 102 | + /// [Region](https://cloud.google.com/compute/docs/regions-zones#identifying_a_region_or_zone) |
| 103 | + /// for the current instance by checking |
| 104 | + /// [instance metadata](https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata). |
| 105 | + /// |
| 106 | + /// If the metadata server cannot be contacted, a [BadConfigurationException] |
| 107 | + /// is thrown. |
| 108 | + Future<String> fromMetadataServer() async { |
| 109 | + const host = 'http://metadata.google.internal/'; |
| 110 | + final url = Uri.parse('$host/computeMetadata/v1/$path'); |
| 111 | + |
| 112 | + try { |
| 113 | + final response = await http.get( |
| 114 | + url, |
| 115 | + headers: {'Metadata-Flavor': 'Google'}, |
| 116 | + ); |
| 117 | + |
| 118 | + if (response.statusCode != 200) { |
| 119 | + throw HttpException( |
| 120 | + '${response.body} (${response.statusCode})', |
| 121 | + uri: url, |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + return response.body; |
| 126 | + } on SocketException catch (e) { |
| 127 | + throw BadConfigurationException( |
| 128 | + ''' |
| 129 | +Could not connect to $host. |
| 130 | +If not running on Google Cloud, one of these environment variables must be set |
| 131 | +to the target region: |
| 132 | +${environmentValues.join('\n')} |
| 133 | +''', |
| 134 | + details: e.toString(), |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments