Skip to content

Commit

Permalink
Hide unused fields for Amazon Web Services connection (#25416)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis authored Aug 4, 2022
1 parent 6861bcc commit 171aaf0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
28 changes: 28 additions & 0 deletions airflow/providers/amazon/aws/hooks/base_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,34 @@ def decorator_f(self, *args, **kwargs):

return retry_decorator

@staticmethod
def get_ui_field_behaviour() -> Dict[str, Any]:
"""Returns custom UI field behaviour for AWS Connection."""
return {
"hidden_fields": ["host", "schema", "port"],
"relabeling": {
"login": "AWS Access Key ID",
"password": "AWS Secret Access Key",
},
"placeholders": {
"login": "AKIAIOSFODNN7EXAMPLE",
"password": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"extra": json.dumps(
{
"region_name": "us-east-1",
"session_kwargs": {"profile_name": "default"},
"config_kwargs": {"retries": {"mode": "standard", "max_attempts": 10}},
"role_arn": "arn:aws:iam::123456789098:role/role-name",
"assume_role_method": "assume_role",
"assume_role_kwargs": {"RoleSessionName": "airflow"},
"aws_session_token": "AQoDYXdzEJr...EXAMPLETOKEN",
"host": "http://localhost:4566",
},
indent=2,
),
},
}

def test_connection(self):
"""
Tests the AWS connection by call AWS STS (Security Token Service) GetCallerIdentity API.
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/amazon/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ extra-links:
connection-types:
- hook-class-name: airflow.providers.amazon.aws.hooks.s3.S3Hook
connection-type: s3
- hook-class-name: airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook
- hook-class-name: airflow.providers.amazon.aws.hooks.base_aws.AwsGenericHook
connection-type: aws
- hook-class-name: airflow.providers.amazon.aws.hooks.emr.EmrHook
connection-type: emr
Expand Down
38 changes: 33 additions & 5 deletions docs/apache-airflow-providers-amazon/connections/aws.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Authenticating to AWS

Authentication may be performed using any of the `boto3 options <https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#configuring-credentials>`_. Alternatively, one can pass credentials in as a Connection initialisation parameter.

To use IAM instance profile, create an "empty" connection (i.e. one with no Login or Password specified, or
``aws://``).
To use IAM instance profile, create an "empty" connection (i.e. one with no AWS Access Key ID or AWS Secret Access Key
specified, or ``aws://``).


Default Connection IDs
Expand All @@ -49,13 +49,13 @@ Configuring the Connection
--------------------------


Login (optional)
AWS Access Key ID (optional)
Specify the AWS access key ID used for the initial connection.
If you do an `assume role <https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html>`__
by specifying a ``role_arn`` in the **Extra** field,
then temporary credentials will be used for subsequent calls to AWS.

Password (optional)
AWS Secret Access Key (optional)
Specify the AWS secret access key used for the initial connection.
If you do an `assume role <https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html>`__
by specifying a ``role_arn`` in the **Extra** field,
Expand Down Expand Up @@ -104,6 +104,34 @@ If you are configuring the connection via a URI, ensure that all components of t
Examples
--------

**Snippet for create Connection as URI**:
.. code-block:: python
import os
from airflow.models.connection import Connection
conn = Connection(
conn_id="sample_aws_connection",
conn_type="aws",
login="AKIAIOSFODNN7EXAMPLE", # Reference to AWS Access Key ID
password="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", # Reference to AWS Secret Access Key
extra={
# Specify extra parameters here
"region_name": "eu-central-1",
},
)
# Generate Environment Variable Name and Connection URI
env_key = f"AIRFLOW_CONN_{conn.conn_id.upper()}"
conn_uri = conn.get_uri()
print(f"{env_key}={conn_uri}")
# AIRFLOW_CONN_SAMPLE_AWS_CONNECTION=aws://AKIAIOSFODNN7EXAMPLE:wJalrXUtnFEMI%2FK7MDENG%2FbPxRfiCYEXAMPLEKEY@/?region_name=eu-central-1
# Test connection
os.environ[env_key] = conn_uri
print(conn.test_connection())
**Using instance profile**:
.. code-block:: bash
Expand All @@ -125,7 +153,7 @@ Examples for the **Extra** field

1. Using *~/.aws/credentials* and *~/.aws/config* file, with a profile.

This assumes all other Connection fields eg **Login** are empty.
This assumes all other Connection fields eg **AWS Access Key ID** or **AWS Secret Access Key** are empty.

.. code-block:: json
Expand Down

0 comments on commit 171aaf0

Please sign in to comment.