You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/06/02 00:25:20 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #8974: Vault has now VaultHook not only SecretBackend

kaxil commented on a change in pull request #8974:
URL: https://github.com/apache/airflow/pull/8974#discussion_r433556742



##########
File path: airflow/providers/hashicorp/hooks/vault.py
##########
@@ -0,0 +1,310 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Hook for HashiCorp Vault"""
+import json
+from typing import Optional, Tuple
+
+import hvac
+from hvac.exceptions import VaultError
+from requests import Response
+
+from airflow.hooks.base_hook import BaseHook
+from airflow.providers.hashicorp.common.vault_client import VaultClient
+
+DEFAULT_KUBERNETES_JWT_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token'
+DEFAULT_KV_ENGINE_VERSION = 2
+
+
+class VaultHook(BaseHook):
+    """
+    HashiCorp Vault wrapper to Interact with HashiCorp Vault KeyValue Secret engine.
+
+    HashiCorp HVac documentation:
+       * https://hvac.readthedocs.io/en/stable/
+
+    You connect to the host specified as host in the connection. The login/password from the connection
+    are used as credentials usually and you can specify different authentication parameters
+    via init params or via corresponding extras in the connection.
+
+    The extras in the connection are named the same as the parameters (`mount_point`,'kv_engine_version' ...).
+
+    Login/Password are used as credentials:
+
+        * approle: password -> secret_id
+        * aws_iam: login -> key_id, password -> secret_id
+        * azure: login -> client_id, password -> client_secret
+        * ldap: login -> username,   password -> password
+        * userpass: login -> username, password -> password
+        * radius: password -> radius_secret
+
+    :param vault_conn_id: The id of the connection to use
+    :type vault_conn_id: str
+    :param auth_type: Authentication Type for Vault. Default is ``token``. Available values are in
+        :py:const:`airflow.providers.hashicorp.common.vault_client.VALID_AUTH_TYPES`.
+    :type auth_type: str
+    :param mount_point: The "path" the secret engine was mounted on. Default depends on the engine used.
+    :type mount_point: str
+    :param kv_engine_version: Select the version of the engine to run (``1`` or ``2``). Default 2.
+    :type kv_engine_version: int
+    :param token: Authentication token to include in requests sent to Vault.
+        (for ``token`` and ``github`` auth_type)
+    :type token: str
+    :param role_id: Role ID for Authentication (for ``approle``, ``aws_iam`` auth_types)
+    :type role_id: str
+    :param kubernetes_role: Role for Authentication (for ``kubernetes`` auth_type)
+    :type kubernetes_role: str
+    :param kubernetes_jwt_path: Path for kubernetes jwt token (for ``kubernetes`` auth_type, default:
+        ``/var/run/secrets/kubernetes.io/serviceaccount/token``)
+    :type kubernetes_jwt_path: str
+    :param gcp_key_path: Path to GCP Credential JSON file (for ``gcp`` auth_type)
+           Mutually exclusive with gcp_keyfile_dict
+    :type gcp_key_path: str
+    :param gcp_keyfile_dict: Dictionary of keyfile parameters. (for ``gcp`` auth_type).
+           Mutually exclusive with gcp_key_path
+    :type gcp_keyfile_dict: dict
+    :param gcp_scopes: Comma-separated string containing GCP scopes (for ``gcp`` auth_type)
+    :type gcp_scopes: str
+    :param azure_tenant_id: Tenant of azure (for ``azure`` auth_type)
+    :type azure_tenant_id: str
+    :param azure_resource: Resource if of azure (for ``azure`` auth_type)
+    :type azure_resource: str
+    :param radius_host: Host for radius (for ``radius`` auth_type)
+    :type radius_host: str
+    :param radius_port: Port for radius (for ``radius`` auth_type)
+    :type radius_port: int
+
+    """
+    def __init__(  # pylint: disable=too-many-arguments
+        self,
+        vault_conn_id: str,
+        auth_type: Optional[str] = None,
+        mount_point: Optional[str] = None,
+        kv_engine_version: Optional[int] = None,
+        token: Optional[str] = None,
+        role_id: Optional[str] = None,
+        kubernetes_role: Optional[str] = None,
+        kubernetes_jwt_path: Optional[str] = None,
+        gcp_key_path: Optional[str] = None,
+        gcp_keyfile_dict: Optional[dict] = None,
+        gcp_scopes: Optional[str] = None,
+        azure_tenant_id: Optional[str] = None,
+        azure_resource: Optional[str] = None,
+        radius_host: Optional[str] = None,
+        radius_port: Optional[int] = None
+    ):
+        super().__init__()
+        self.connection = self.get_connection(vault_conn_id)
+
+        if not auth_type:
+            auth_type = self.connection.extra_dejson.get('auth_type')
+        if not auth_type:
+            auth_type = "token"
+
+        if not mount_point:
+            mount_point = self.connection.extra_dejson.get('mount_point')
+
+        if not kv_engine_version:
+            conn_version = self.connection.extra_dejson.get("kv_engine_version")
+            try:
+                kv_engine_version = int(conn_version) if conn_version else DEFAULT_KV_ENGINE_VERSION
+            except ValueError:
+                raise VaultError(f"The version is not an int: {conn_version}. ")
+
+        if not role_id:
+            role_id = self.connection.extra_dejson.get('role_id')
+
+        if not token:
+            token = self.connection.extra_dejson.get('token')
+
+        azure_resource, azure_tenant_id = \
+            self._get_azure_parameters_from_connection(azure_resource, azure_tenant_id)
+        gcp_key_path, gcp_keyfile_dict, gcp_scopes = \
+            self._get_gcp_parameters_from_connection(gcp_key_path, gcp_keyfile_dict, gcp_scopes)
+        kubernetes_jwt_path, kubernetes_role = \
+            self._get_kubernetes_parameters_from_connection(kubernetes_jwt_path, kubernetes_role)
+        radius_host, radius_port = self._get_radius_parameters_from_connection(radius_host, radius_port)

Review comment:
       These methods would be called for all auth_types i.e. even if the auth is just token, it would call all these functions and try to get values from Connection




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org