| 
 | 1 | +from __future__ import annotations  | 
 | 2 | + | 
 | 3 | +from typing import Any, Literal  | 
 | 4 | + | 
 | 5 | +from aws_lambda_powertools.utilities.data_classes.common import (  | 
 | 6 | +    DictWrapper,  | 
 | 7 | +)  | 
 | 8 | + | 
 | 9 | + | 
 | 10 | +class TransferFamilyAuthorizer(DictWrapper):  | 
 | 11 | +    @property  | 
 | 12 | +    def username(self) -> str:  | 
 | 13 | +        """The username used for authentication"""  | 
 | 14 | +        return self["username"]  | 
 | 15 | + | 
 | 16 | +    @property  | 
 | 17 | +    def password(self) -> str | None:  | 
 | 18 | +        """  | 
 | 19 | +        The password used for authentication.  | 
 | 20 | +        None in case customer authenticating with certificates  | 
 | 21 | +        """  | 
 | 22 | +        return self["password"]  | 
 | 23 | + | 
 | 24 | +    @property  | 
 | 25 | +    def protocol(self) -> str:  | 
 | 26 | +        """The protocol can be SFTP, FTP or FTPS"""  | 
 | 27 | +        return self["protocol"]  | 
 | 28 | + | 
 | 29 | +    @property  | 
 | 30 | +    def server_id(self) -> str:  | 
 | 31 | +        """The AWS Transfer Family ServerID"""  | 
 | 32 | +        return self["serverId"]  | 
 | 33 | + | 
 | 34 | +    @property  | 
 | 35 | +    def source_ip(self) -> str:  | 
 | 36 | +        """The customer IP used for connection"""  | 
 | 37 | +        return self["sourceIp"]  | 
 | 38 | + | 
 | 39 | + | 
 | 40 | +class TransferFamilyAuthorizerResponse:  | 
 | 41 | + | 
 | 42 | +    def _build_authentication_response(  | 
 | 43 | +        self,  | 
 | 44 | +        role_arn: str,  | 
 | 45 | +        policy: str | None = None,  | 
 | 46 | +        home_directory: str | None = None,  | 
 | 47 | +        home_directory_details: dict | None = None,  | 
 | 48 | +        home_directory_type: Literal["LOGICAL", "PATH"] = "PATH",  | 
 | 49 | +        user_gid: int | None = None,  | 
 | 50 | +        user_uid: int | None = None,  | 
 | 51 | +        public_keys: str | None = None,  | 
 | 52 | +    ) -> dict[str, Any]:  | 
 | 53 | + | 
 | 54 | +        response: dict[str, Any] = {}  | 
 | 55 | + | 
 | 56 | +        if home_directory_type == "PATH":  | 
 | 57 | +            if not home_directory:  | 
 | 58 | +                raise ValueError("home_directory must be set when home_directory_type is PATH")  | 
 | 59 | + | 
 | 60 | +            response["HomeDirectory"] = home_directory  | 
 | 61 | +        elif home_directory_type == "LOGICAL":  | 
 | 62 | +            if not home_directory_details:  | 
 | 63 | +                raise ValueError("home_directory_details must be set when home_directory_type is LOGICAL")  | 
 | 64 | + | 
 | 65 | +            response["HomeDirectoryDetails"] = [home_directory_details]  | 
 | 66 | + | 
 | 67 | +        else:  | 
 | 68 | +            raise ValueError(f"Invalid home_directory_type: {home_directory_type}")  | 
 | 69 | + | 
 | 70 | +        if user_uid is not None:  | 
 | 71 | +            response["PosixProfile"] = {"Gid": user_gid, "Uid": user_gid}  | 
 | 72 | + | 
 | 73 | +        if policy:  | 
 | 74 | +            response["Policy"] = policy  | 
 | 75 | + | 
 | 76 | +        if public_keys:  | 
 | 77 | +            response["PublicKeys"] = public_keys  | 
 | 78 | + | 
 | 79 | +        response["Role"] = role_arn  | 
 | 80 | +        response["HomeDirectoryType"] = home_directory_type  | 
 | 81 | + | 
 | 82 | +        return response  | 
 | 83 | + | 
 | 84 | +    def build_authentication_response_efs(  | 
 | 85 | +        self,  | 
 | 86 | +        role_arn: str,  | 
 | 87 | +        user_gid: int,  | 
 | 88 | +        user_uid: int,  | 
 | 89 | +        policy: str | None = None,  | 
 | 90 | +        home_directory: str | None = None,  | 
 | 91 | +        home_directory_details: dict | None = None,  | 
 | 92 | +        home_directory_type: Literal["LOGICAL", "PATH"] = "PATH",  | 
 | 93 | +        public_keys: str | None = None,  | 
 | 94 | +    ) -> dict[str, Any]:  | 
 | 95 | +        """  | 
 | 96 | +        Build an authentication response for AWS Transfer Family using EFS (Elastic File System).  | 
 | 97 | +
  | 
 | 98 | +        Parameters:  | 
 | 99 | +        -----------  | 
 | 100 | +        role_arn : str  | 
 | 101 | +            The Amazon Resource Name (ARN) of the IAM role.  | 
 | 102 | +        user_gid : int  | 
 | 103 | +            The group ID of the user.  | 
 | 104 | +        user_uid : int  | 
 | 105 | +            The user ID.  | 
 | 106 | +        policy : str | None, optional  | 
 | 107 | +            The IAM policy document. Defaults to None.  | 
 | 108 | +        home_directory : str | None, optional  | 
 | 109 | +            The home directory path. Required if home_directory_type is "PATH". Defaults to None.  | 
 | 110 | +        home_directory_details : dict | None, optional  | 
 | 111 | +            Details of the home directory. Required if home_directory_type is "LOGICAL". Defaults to None.  | 
 | 112 | +        home_directory_type : Literal["LOGICAL", "PATH"], optional  | 
 | 113 | +            The type of home directory. Must be either "LOGICAL" or "PATH". Defaults to "PATH".  | 
 | 114 | +        public_keys : str | None, optional  | 
 | 115 | +            The public keys associated with the user. Defaults to None.  | 
 | 116 | +
  | 
 | 117 | +        Returns:  | 
 | 118 | +        --------  | 
 | 119 | +        dict[str, Any]  | 
 | 120 | +            A dictionary containing the authentication response with various details such as  | 
 | 121 | +            role ARN, policy, home directory information, and user details.  | 
 | 122 | +
  | 
 | 123 | +        Raises:  | 
 | 124 | +        -------  | 
 | 125 | +        ValueError  | 
 | 126 | +            If an invalid home_directory_type is provided or if required parameters are missing  | 
 | 127 | +            for the specified home_directory_type.  | 
 | 128 | +        """  | 
 | 129 | + | 
 | 130 | +        return self._build_authentication_response(  | 
 | 131 | +            role_arn=role_arn,  | 
 | 132 | +            policy=policy,  | 
 | 133 | +            home_directory=home_directory,  | 
 | 134 | +            home_directory_details=home_directory_details,  | 
 | 135 | +            home_directory_type=home_directory_type,  | 
 | 136 | +            public_keys=public_keys,  | 
 | 137 | +            user_gid=user_gid,  | 
 | 138 | +            user_uid=user_uid,  | 
 | 139 | +        )  | 
 | 140 | + | 
 | 141 | +    def build_authentication_response_s3(  | 
 | 142 | +        self,  | 
 | 143 | +        role_arn: str,  | 
 | 144 | +        policy: str | None = None,  | 
 | 145 | +        home_directory: str | None = None,  | 
 | 146 | +        home_directory_details: dict | None = None,  | 
 | 147 | +        home_directory_type: Literal["LOGICAL", "PATH"] = "PATH",  | 
 | 148 | +        public_keys: str | None = None,  | 
 | 149 | +    ) -> dict[str, Any]:  | 
 | 150 | +        """  | 
 | 151 | +        Build an authentication response for Amazon S3.  | 
 | 152 | +
  | 
 | 153 | +        This method constructs an authentication response tailored for S3 access,  | 
 | 154 | +        likely by calling an internal method with the provided parameters.  | 
 | 155 | +
  | 
 | 156 | +        Parameters:  | 
 | 157 | +        -----------  | 
 | 158 | +        role_arn : str  | 
 | 159 | +            The Amazon Resource Name (ARN) of the IAM role for S3 access.  | 
 | 160 | +        policy : str | None, optional  | 
 | 161 | +            The IAM policy document for S3 access. Defaults to None.  | 
 | 162 | +        home_directory : str | None, optional  | 
 | 163 | +            The home directory path in S3. Required if home_directory_type is "PATH". Defaults to None.  | 
 | 164 | +        home_directory_details : dict | None, optional  | 
 | 165 | +            Details of the home directory in S3. Required if home_directory_type is "LOGICAL". Defaults to None.  | 
 | 166 | +        home_directory_type : Literal["LOGICAL", "PATH"], optional  | 
 | 167 | +            The type of home directory in S3. Must be either "LOGICAL" or "PATH". Defaults to "PATH".  | 
 | 168 | +        public_keys : str | None, optional  | 
 | 169 | +            The public keys associated with the user for S3 access. Defaults to None.  | 
 | 170 | +
  | 
 | 171 | +        Returns:  | 
 | 172 | +        --------  | 
 | 173 | +        dict[str, Any]  | 
 | 174 | +            A dictionary containing the authentication response with various details such as  | 
 | 175 | +            role ARN, policy, home directory information, and potentially other S3-specific attributes.  | 
 | 176 | +
  | 
 | 177 | +        Raises:  | 
 | 178 | +        -------  | 
 | 179 | +        ValueError  | 
 | 180 | +            If an invalid home_directory_type is provided or if required parameters are missing  | 
 | 181 | +            for the specified home_directory_type.  | 
 | 182 | +        """  | 
 | 183 | +        return self._build_authentication_response(  | 
 | 184 | +            role_arn=role_arn,  | 
 | 185 | +            policy=policy,  | 
 | 186 | +            home_directory=home_directory,  | 
 | 187 | +            home_directory_details=home_directory_details,  | 
 | 188 | +            home_directory_type=home_directory_type,  | 
 | 189 | +            public_keys=public_keys,  | 
 | 190 | +        )  | 
0 commit comments