|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# Copyright 2021 IBM All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from requests import Request |
| 20 | + |
| 21 | +from ..token_managers.vpc_instance_token_manager import VPCInstanceTokenManager |
| 22 | +from .authenticator import Authenticator |
| 23 | + |
| 24 | + |
| 25 | +class VPCInstanceAuthenticator(Authenticator): |
| 26 | + """VPCInstanceAuthenticator implements an authentication scheme in which it |
| 27 | + retrieves an "instance identity token" and exchanges that |
| 28 | + for an IAM access token using the VPC Instance Metadata Service API which is available |
| 29 | + on the local compute resource (VM). |
| 30 | + The instance identity token is similar to an IAM apikey, except that it is managed |
| 31 | + automatically by the compute resource provider (VPC). |
| 32 | + The resulting IAM access token is then added to outbound requests in an Authorization header of the form: |
| 33 | +
|
| 34 | + Authorization: Bearer <access-token> |
| 35 | +
|
| 36 | + Keyword Arguments: |
| 37 | + iam_profile_crn (str, optional): |
| 38 | + The CRN of the linked trusted IAM profile to be used as the identity of the compute resource. |
| 39 | + At most one of iam_profile_crn or iam_profile_id may be specified. If neither one is specified, |
| 40 | + then the default IAM profile defined for the compute resource will be used. Defaults to None. |
| 41 | + iam_profile_id (str, optional): |
| 42 | + The ID of the linked trusted IAM profile to be used when obtaining the IAM access token. |
| 43 | + At most one of iamProfileCrn or iamProfileId may be specified. If neither one is specified, |
| 44 | + then the default IAM profile defined for the compute resource will be used. Defaults to None. |
| 45 | + url (str, optional): |
| 46 | + The VPC Instance Metadata Service's base endpoint URL. Defaults to 'http://169.254.169.254'. |
| 47 | +
|
| 48 | + Attributes: |
| 49 | + iam_profile_crn (str, optional): The CRN of the linked trusted IAM profile. |
| 50 | + iam_profile_id (str, optional): The ID of the linked trusted IAM profile. |
| 51 | + url (str, optional): The VPC Instance Metadata Service's base endpoint URL. |
| 52 | + """ |
| 53 | + |
| 54 | + DEFAULT_IMS_ENDPOINT = 'http://169.254.169.254' |
| 55 | + |
| 56 | + def __init__(self, |
| 57 | + iam_profile_crn: Optional[str] = None, |
| 58 | + iam_profile_id: Optional[str] = None, |
| 59 | + url: Optional[str] = None) -> None: |
| 60 | + |
| 61 | + if not url: |
| 62 | + url = self.DEFAULT_IMS_ENDPOINT |
| 63 | + |
| 64 | + self.token_manager = VPCInstanceTokenManager( |
| 65 | + url=url, iam_profile_crn=iam_profile_crn, iam_profile_id=iam_profile_id) |
| 66 | + |
| 67 | + self.validate() |
| 68 | + |
| 69 | + def authentication_type(self) -> str: |
| 70 | + """Returns this authenticator's type ('VPC').""" |
| 71 | + return Authenticator.AUTHTYPE_VPC |
| 72 | + |
| 73 | + def validate(self) -> None: |
| 74 | + super().validate() |
| 75 | + |
| 76 | + if self.token_manager.iam_profile_crn and self.token_manager.iam_profile_id: |
| 77 | + raise ValueError( |
| 78 | + 'At most one of "iam_profile_id" or "iam_profile_crn" may be specified.') |
| 79 | + |
| 80 | + def authenticate(self, req: Request) -> None: |
| 81 | + """Adds IAM authentication information to the request. |
| 82 | +
|
| 83 | + The IAM access token will be added to the request's headers in the form: |
| 84 | +
|
| 85 | + Authorization: Bearer <bearer-token> |
| 86 | +
|
| 87 | + Args: |
| 88 | + req: The request to add IAM authentication information too. Must contain a key to a dictionary |
| 89 | + called headers. |
| 90 | + """ |
| 91 | + headers = req.get('headers') |
| 92 | + bearer_token = self.token_manager.get_token() |
| 93 | + headers['Authorization'] = 'Bearer {0}'.format(bearer_token) |
| 94 | + |
| 95 | + |
| 96 | + def set_iam_profile_crn(self, iam_profile_crn: str) -> None: |
| 97 | + """Sets CRN of the IAM profile. |
| 98 | +
|
| 99 | + Args: |
| 100 | + iam_profile_crn (str): the CRN of the linked trusted IAM profile to be used as |
| 101 | + the identity of the compute resource. |
| 102 | +
|
| 103 | + Raises: |
| 104 | + ValueError: At most one of iam_profile_crn or iam_profile_id may be specified. |
| 105 | + If neither one is specified, then the default IAM profile defined |
| 106 | + for the compute resource will be used. |
| 107 | + """ |
| 108 | + self.token_manager.set_iam_profile_crn(iam_profile_crn) |
| 109 | + self.validate() |
| 110 | + |
| 111 | + def set_iam_profile_id(self, iam_profile_id: str) -> None: |
| 112 | + """Sets the ID of the IAM profile. |
| 113 | +
|
| 114 | + Args: |
| 115 | + iam_profile_id (str): id of the linked trusted IAM profile to be used when obtaining |
| 116 | + the IAM access token |
| 117 | +
|
| 118 | + Raises: |
| 119 | + ValueError: At most one of iam_profile_crn or iam_profile_id may be specified. |
| 120 | + If neither one is specified, then the default IAM profile defined |
| 121 | + for the compute resource will be used. |
| 122 | + """ |
| 123 | + self.token_manager.set_iam_profile_id(iam_profile_id) |
| 124 | + self.validate() |
0 commit comments