|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import base64 |
| 16 | +import json |
15 | 17 | import unittest |
16 | 18 | from collections import OrderedDict |
17 | 19 | from unittest.mock import mock_open, patch |
|
25 | 27 | ResourceAttributes, |
26 | 28 | ) |
27 | 29 |
|
| 30 | + |
| 31 | +def _bearer_jwt(payload: dict) -> str: |
| 32 | + header = base64.urlsafe_b64encode(b'{"alg":"RS256"}').rstrip(b"=").decode() |
| 33 | + body = ( |
| 34 | + base64.urlsafe_b64encode(json.dumps(payload).encode()) |
| 35 | + .rstrip(b"=") |
| 36 | + .decode() |
| 37 | + ) |
| 38 | + return f"Bearer {header}.{body}.fakesig" |
| 39 | + |
| 40 | + |
28 | 41 | MockEksResourceAttributes = { |
29 | 42 | ResourceAttributes.CLOUD_PROVIDER: CloudProviderValues.AWS.value, |
30 | 43 | ResourceAttributes.CLOUD_PLATFORM: CloudPlatformValues.AWS_EKS.value, |
@@ -138,3 +151,52 @@ def test_if_no_eks_paths_should_not_raise( |
138 | 151 | AwsEksResourceDetector(raise_on_error=True).detect() |
139 | 152 | except RuntimeError: |
140 | 153 | self.fail("Should not raise") |
| 154 | + |
| 155 | + @patch( |
| 156 | + "opentelemetry.sdk.extension.aws.resource.eks._get_k8s_cred_value", |
| 157 | + return_value=_bearer_jwt( |
| 158 | + {"iss": "https://oidc.eks.eu-west-2.amazonaws.com/id/EXAMPLE123"} |
| 159 | + ), |
| 160 | + ) |
| 161 | + @patch( |
| 162 | + "opentelemetry.sdk.extension.aws.resource.eks._is_k8s", |
| 163 | + return_value=True, |
| 164 | + ) |
| 165 | + @patch( |
| 166 | + "opentelemetry.sdk.extension.aws.resource.eks._get_cluster_info", |
| 167 | + return_value=f"""{{ |
| 168 | + "data": {{ |
| 169 | + "cluster.name": "{MockEksResourceAttributes[ResourceAttributes.K8S_CLUSTER_NAME]}" |
| 170 | + }} |
| 171 | +}} |
| 172 | +""", |
| 173 | + ) |
| 174 | + @patch( |
| 175 | + "builtins.open", |
| 176 | + new_callable=mock_open, |
| 177 | + read_data=f"14:name=systemd:/docker/{MockEksResourceAttributes[ResourceAttributes.CONTAINER_ID]}\n", |
| 178 | + ) |
| 179 | + def test_eks_oidc_jwt_detected( |
| 180 | + self, |
| 181 | + mock_open_function, |
| 182 | + mock_get_cluster_info, |
| 183 | + mock_is_k8s, |
| 184 | + mock_get_k8s_cred_value, |
| 185 | + ): |
| 186 | + actual = AwsEksResourceDetector().detect() |
| 187 | + self.assertEqual( |
| 188 | + actual.attributes.get(ResourceAttributes.CLOUD_PLATFORM), |
| 189 | + CloudPlatformValues.AWS_EKS.value, |
| 190 | + ) |
| 191 | + |
| 192 | + @patch( |
| 193 | + "opentelemetry.sdk.extension.aws.resource.eks._get_k8s_cred_value", |
| 194 | + return_value=_bearer_jwt({"iss": "https://accounts.google.com"}), |
| 195 | + ) |
| 196 | + @patch( |
| 197 | + "opentelemetry.sdk.extension.aws.resource.eks._is_k8s", |
| 198 | + return_value=True, |
| 199 | + ) |
| 200 | + def test_non_eks_jwt_returns_empty(self, mock_is_k8s, mock_get_k8s_cred_value): |
| 201 | + actual = AwsEksResourceDetector().detect() |
| 202 | + self.assertEqual(actual.attributes, {}) |
0 commit comments