Skip to content

Commit bbe9fe3

Browse files
authored
Simple Vault resolver to generate tokens (#11)
1 parent e1cf0ca commit bbe9fe3

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

himl/secret_resolvers.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import logging
1212
from .simplessm import SimpleSSM
1313
from .simples3 import SimpleS3
14+
from .simplevault import SimpleVault
1415

1516

1617
class SecretResolver:
@@ -65,17 +66,18 @@ def resolve(self, secret_type, secret_params):
6566
return s3.get(bucket, path, base64Encode)
6667

6768

68-
# TODO - vault resolver
6969
class VaultSecretResolver(SecretResolver):
7070
def supports(self, secret_type):
71-
return False
71+
return secret_type == "vault"
7272

7373
def resolve(self, secret_type, secret_params):
74-
return None
74+
# Generate a token for a policy
75+
policy = self.get_param_or_exception("token_policy", secret_params)
76+
vault = SimpleVault
77+
return vault().get_token(policy)
7578

7679

7780
class AggregatedSecretResolver(SecretResolver):
78-
7981
def __init__(self, default_aws_profile=None):
8082
self.secret_resolvers = (SSMSecretResolver(default_aws_profile), S3SecretResolver(default_aws_profile), VaultSecretResolver())
8183

himl/simplessm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import os
1414
from botocore.exceptions import ClientError
1515

16-
1716
logger = logging.getLogger(__name__)
1817

18+
1919
class SimpleSSM(object):
2020
def __init__(self, aws_profile, region_name):
2121
self.initial_aws_profile = os.getenv('AWS_PROFILE', None)

himl/simplevault.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2019 Adobe. All rights reserved.
2+
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License. You may obtain a copy
4+
# of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
6+
# Unless required by applicable law or agreed to in writing, software distributed under
7+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
# OF ANY KIND, either express or implied. See the License for the specific language
9+
# governing permissions and limitations under the License.
10+
11+
import os
12+
import logging
13+
import hvac
14+
15+
16+
logger = logging.getLogger(__name__)
17+
18+
19+
class SimpleVault:
20+
def __init__(self):
21+
pass
22+
23+
def get_vault_client(self):
24+
url = os.getenv('VAULT_ADDR')
25+
namespace = os.getenv('VAULT_NAMESPACE')
26+
username = os.getenv('VAULT_USERNAME')
27+
password = os.getenv('VAULT_PASSWORD')
28+
logger.info("Vault using url: {}, namespace: {}, username: {}".format(url, namespace, username))
29+
30+
client = hvac.Client(
31+
url=url,
32+
namespace=namespace,
33+
)
34+
35+
try:
36+
client.auth.ldap.login(
37+
username=username,
38+
password=password,
39+
)
40+
assert client.is_authenticated()
41+
logger.info("Vault LDAP authenticated")
42+
except Exception as e:
43+
raise Exception("Error authenticating Vault over LDAP")
44+
45+
return client
46+
47+
def get_token(self, policy):
48+
role = os.getenv('VAULT_ROLE')
49+
client = self.get_vault_client()
50+
logger.info("Generating token for policy: {} using role: {}".format(policy, role))
51+
52+
token = client.create_token(
53+
policies=[policy],
54+
role=role,
55+
lease='24h',
56+
)
57+
58+
return token['auth']['client_token']

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ backports.functools_lru_cache>=1.5
44
pathlib2>=2.3.4
55
boto3>=1.9.110
66
pyyaml>=5.1
7+
botocore>=1.12
8+
hvac>=0.9.3

0 commit comments

Comments
 (0)