Skip to content

Commit

Permalink
Make BaseSecretsBackend.build_path generic (#7948)
Browse files Browse the repository at this point in the history
Currently the arguments required for it are `connections_prefix` and `conn_id` . Changing this to `path_prefix` and `secret_id` allows using that method for retrieving variables too
  • Loading branch information
kaxil committed Mar 28, 2020
1 parent 1bdd5ff commit eb4af4f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion airflow/providers/amazon/aws/secrets/systems_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_conn_uri(self, conn_id: str) -> Optional[str]:
:type conn_id: str
"""

ssm_path = self.build_path(connections_prefix=self.connections_prefix, conn_id=conn_id)
ssm_path = self.build_path(self.connections_prefix, conn_id)
try:
response = self.client.get_parameter(
Name=ssm_path, WithDecryption=False
Expand Down
6 changes: 1 addition & 5 deletions airflow/providers/google/cloud/secrets/secrets_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ def get_conn_uri(self, conn_id: str) -> Optional[str]:
:param conn_id: connection id
:type conn_id: str
"""
secret_id = self.build_path(
connections_prefix=self.connections_prefix,
conn_id=conn_id,
sep=self.sep
)
secret_id = self.build_path(self.connections_prefix, conn_id, self.sep)
# always return the latest version of the secret
secret_version = "latest"
name = self.client.secret_version_path(self.project_id, secret_id, secret_version)
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/hashicorp/secrets/vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def get_conn_uri(self, conn_id: str) -> Optional[str]:
:param conn_id: connection id
:type conn_id: str
"""
secret_path = self.build_path(connections_prefix=self.connections_path, conn_id=conn_id)
secret_path = self.build_path(self.connections_path, conn_id)

try:
if self.kv_engine_version == 1:
Expand Down
12 changes: 6 additions & 6 deletions airflow/secrets/base_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ def __init__(self, **kwargs):
pass

@staticmethod
def build_path(connections_prefix: str, conn_id: str, sep: str = "/") -> str:
def build_path(path_prefix: str, secret_id: str, sep: str = "/") -> str:
"""
Given conn_id, build path for Secrets Backend
:param connections_prefix: prefix of the secret to read to get Connections
:type connections_prefix: str
:param conn_id: connection id
:type conn_id: str
:param path_prefix: Prefix of the path to get secret
:type path_prefix: str
:param secret_id: Secret id
:type secret_id: str
:param sep: separator used to concatenate connections_prefix and conn_id. Default: "/"
:type sep: str
"""
return f"{connections_prefix}{sep}{conn_id}"
return f"{path_prefix}{sep}{secret_id}"

def get_conn_uri(self, conn_id: str) -> Optional[str]:
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/secrets/test_secrets_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def tearDown(self) -> None:
clear_db_variables()

@parameterized.expand([
('default', {"connections_prefix": "PREFIX", "conn_id": "ID"}, "PREFIX/ID"),
('with_sep', {"connections_prefix": "PREFIX", "conn_id": "ID", "sep": "-"}, "PREFIX-ID")
('default', {"path_prefix": "PREFIX", "secret_id": "ID"}, "PREFIX/ID"),
('with_sep', {"path_prefix": "PREFIX", "secret_id": "ID", "sep": "-"}, "PREFIX-ID")
])
def test_build_path(self, _, kwargs, output):
build_path = BaseSecretsBackend.build_path
Expand Down

0 comments on commit eb4af4f

Please sign in to comment.