Skip to content

Commit aec995c

Browse files
authored
Merge pull request #154 from britive/develop
v1.8.0rc2
2 parents 932b5fc + 3a07824 commit aec995c

File tree

7 files changed

+84
-57
lines changed

7 files changed

+84
-57
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
* As of v1.4.0 release candidates will be published in an effort to get new features out faster while still allowing
44
time for full QA testing before moving the release candidate to a full release.
55

6+
## v1.8.0rc2 [2024-06-07]
7+
8+
__What's New:__
9+
10+
* Added a new global config setting for CA bundle certificates.
11+
12+
__Enhancements:__
13+
14+
* Added new `ca_bundle` global setting for user provided CA bundle certs.
15+
16+
__Bug Fixes:__
17+
18+
* Switched `pybritive-kube-exec` to full path in for kube config.
19+
20+
__Dependencies:__
21+
22+
* `britive>=2.25.0rc4`
23+
24+
__Other:__
25+
26+
* None
27+
628
## v1.8.0rc1 [2024-06-03]
729

830
__What's New:__

docs/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ As such, any functionality of `requests` can be used, including setting an HTTP
7171
7272
### Custom TLS Certificates
7373

74+
This can be set in the `pybritive.config` `global` settings by setting `ca_bundle`, e.g.:
75+
76+
```toml
77+
[global]
78+
default_tenant=tenant
79+
output_format=json
80+
credential_backend=file
81+
# replace "/location/of/the/CA_BUNDLE_FILE.pem" with the path to the desired CA bundle file
82+
ca_bundle=/location/of/the/CA_BUNDLE_FILE.pem
83+
```
84+
7485
Setting custom TLS certificates functionality of `requests` can also be used.
7586

7687
* Certificate bundles will be set via environment variables.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
beautifulsoup4
22
boto3
3-
britive>=2.25.0rc3
3+
britive>=2.25.0rc4
44
certifi
55
charset-normalizer
66
click~=8.1.7

src/pybritive/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.8.0rc1'
1+
__version__ = '1.8.0rc2'

src/pybritive/helpers/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def coalesce(*arg):
4343
'credential_backend',
4444
'auto-refresh-profile-cache',
4545
'auto-refresh-kube-config',
46+
'ca_bundle',
4647
]
4748

4849
tenant_fields = ['name', 'output_format', 'sso_idp']
@@ -69,6 +70,7 @@ def __init__(self, cli: object, tenant_name: str = None):
6970
self.loaded = False
7071
self.validation_error_messages = []
7172
self.gcloud_key_file_path: str = str(Path(self.path).parent / 'pybritive-gcloud-key-files')
73+
self.global_ca_bundle = None
7274

7375
def clear_gcloud_auth_key_files(self, profile=None):
7476
path = Path(self.gcloud_key_file_path)
@@ -123,6 +125,7 @@ def load(self, force=False):
123125
self.tenants_by_name[name] = item
124126
self.aliases_and_names = {**self.tenants, **self.tenants_by_name}
125127
self.profile_aliases = self.config.get('profile-aliases', {})
128+
self.global_ca_bundle = self.config.get('ca_bundle', {})
126129
self.loaded = True
127130

128131
def get_tenant(self):
@@ -300,6 +303,11 @@ def validate_global(self, section, fields):
300303
if value not in tenant_aliases_from_sections:
301304
error = f'Invalid {section} field {field} value {value} provided. Tenant not found.'
302305
self.validation_error_messages.append(error)
306+
if field == 'ca_bundle':
307+
ca_bundle_file_path = Path(value).expanduser()
308+
if not Path.is_file(ca_bundle_file_path):
309+
error = f'Invalid {field} file {ca_bundle_file_path}. File does not exist.'
310+
self.validation_error_messages.append(error)
303311

304312
def validate_profile_aliases(self, section, fields):
305313
for field, value in fields.items():

src/pybritive/helpers/credentials.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ def _setup_requests_session(self):
7474
self.session = requests.Session()
7575
retries = Retry(total=5, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
7676
self.session.mount('https://', HTTPAdapter(max_retries=retries))
77-
77+
global_ca_bundle = self.cli.config.get_tenant().get('ca_bundle')
78+
if global_ca_bundle and not os.getenv('REQUESTS_CA_BUNDLE', os.getenv('CURL_CA_BUNDLE')):
79+
os.environ['PYBRITIVE_CA_BUNDLE'] = global_ca_bundle
80+
self.session.verify = global_ca_bundle
7881
# allow the disabling of TLS/SSL verification for testing in development (mostly local development)
7982
if os.getenv('BRITIVE_NO_VERIFY_SSL') and '.dev.' in self.tenant:
8083
# turn off ssl verification

src/pybritive/helpers/kube_config_builder.py

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import click
2-
import yaml
1+
import base64
2+
import os
33
from pathlib import Path
4+
import shutil
5+
import yaml
46
from .config import ConfigManager
57
from ..britive_cli import BritiveCli
6-
import os
7-
import base64
88

99

1010
def sanitize(name: str):
@@ -19,7 +19,7 @@ def check_env_var(filename, cli: BritiveCli):
1919
# no env var present
2020
if not kubeconfig:
2121
command = f'export KUBECONFIG=~/.kube/config:{filename}'
22-
cli.print(f'Please ensure your KUBECONFIG environment variable includes the Britive managed kube config file.')
22+
cli.print('Please ensure your KUBECONFIG environment variable includes the Britive managed kube config file.')
2323
cli.print(command)
2424
else:
2525
for configfile in kubeconfig.split(':'):
@@ -28,8 +28,7 @@ def check_env_var(filename, cli: BritiveCli):
2828
return # we found what we came for - silently continue
2929

3030
# if we get here we need to instruct the user to add the britive managed kube config file
31-
cli.print(f'Please modify your KUBECONFIG environment variable to include the '
32-
f'Britive managed kube config file.')
31+
cli.print('Please modify your KUBECONFIG environment variable to include the Britive managed kube config file.')
3332
command = f'export KUBECONFIG="${{KUBECONFIG}}:{filename}"'
3433
cli.print(command)
3534

@@ -40,7 +39,7 @@ def merge_new_with_existing(clusters, contexts, users, filename, tenant):
4039
# them with the above created items
4140
existing_kubeconfig = {}
4241
if Path(filename).exists():
43-
with open(filename, 'r') as f:
42+
with open(filename, 'r', encoding='utf-8') as f:
4443
existing_kubeconfig = yaml.safe_load(f) or {}
4544

4645
prefix = f'{tenant}-'
@@ -57,16 +56,10 @@ def merge_new_with_existing(clusters, contexts, users, filename, tenant):
5756
if not user.get('name', '').startswith(prefix):
5857
users.append(user)
5958

60-
kubeconfig = {
61-
'apiVersion': 'v1',
62-
'clusters': clusters,
63-
'contexts': contexts,
64-
'users': users,
65-
'kind': 'Config'
66-
}
59+
kubeconfig = {'apiVersion': 'v1', 'clusters': clusters, 'contexts': contexts, 'users': users, 'kind': 'Config'}
6760

6861
# write out the config file
69-
with open(filename, 'w') as f:
62+
with open(filename, 'w', encoding='utf-8') as f:
7063
yaml.safe_dump(kubeconfig, f, default_flow_style=False, encoding='utf-8')
7164

7265

@@ -90,7 +83,7 @@ def parse_profiles(profiles, aliases):
9083
'cert': profile['cert'],
9184
'escaped_profile': escaped_profile_str,
9285
'profile': f"{profile['app']}/{profile['env']}/{profile['profile']}".lower(),
93-
'alias': alias
86+
'alias': alias,
9487
}
9588
cluster_names[env_profile]['apps'].append(sanitize(profile['app']))
9689
return [cluster_names, assigned_aliases]
@@ -110,24 +103,28 @@ def valid_cert(cert: str, profile: str, cli: BritiveCli):
110103

111104

112105
def build_tenant_config(tenant, cluster_names, username, cli: BritiveCli):
113-
users = [
114-
{
115-
'name': username,
116-
'user': {
117-
'exec': {
118-
'apiVersion': 'client.authentication.k8s.io/v1beta1',
119-
'command': 'pybritive-kube-exec',
120-
'args': [
121-
'-t',
122-
tenant
123-
],
124-
'env': None,
125-
'interactiveMode': 'Never',
126-
'provideClusterInfo': True
127-
}
106+
kube_exec_full_path = shutil.which('pybritive-kube-exec')
107+
if not kube_exec_full_path:
108+
kube_exec_full_path = 'pybritive-kube-exec'
109+
users = (
110+
[
111+
{
112+
'name': username,
113+
'user': {
114+
'exec': {
115+
'apiVersion': 'client.authentication.k8s.io/v1beta1',
116+
'command': kube_exec_full_path,
117+
'args': ['-t', tenant],
118+
'env': None,
119+
'interactiveMode': 'Never',
120+
'provideClusterInfo': True,
121+
}
122+
},
128123
}
129-
}
130-
] if len(cluster_names.keys()) > 0 else []
124+
]
125+
if len(cluster_names.keys()) > 0
126+
else []
127+
)
131128
contexts = []
132129
clusters = []
133130

@@ -153,22 +150,17 @@ def build_tenant_config(tenant, cluster_names, username, cli: BritiveCli):
153150
'extensions': [
154151
{
155152
'name': 'client.authentication.k8s.io/exec',
156-
'extension': {
157-
'britive-profile': details.get('alias') or details['escaped_profile']
158-
}
153+
'extension': {'britive-profile': details.get('alias') or details['escaped_profile']},
159154
}
160-
]
161-
}
155+
],
156+
},
162157
}
163158
)
164159

165160
contexts.append(
166161
{
167162
'name': details.get('alias') or f'{tenant}-{name}',
168-
'context': {
169-
'cluster': f'{tenant}-{name}',
170-
'user': username
171-
}
163+
'context': {'cluster': f'{tenant}-{name}', 'user': username},
172164
}
173165
)
174166
return [clusters, contexts, users]
@@ -189,10 +181,7 @@ def build_kube_config(profiles: list, config: ConfigManager, username: str, cli:
189181

190182
# establish the 3 elements of the config
191183
clusters, contexts, users = build_tenant_config(
192-
tenant=tenant,
193-
cluster_names=cluster_names,
194-
username=username,
195-
cli=cli
184+
tenant=tenant, cluster_names=cluster_names, username=username, cli=cli
196185
)
197186

198187
# calculate the path for the config
@@ -202,13 +191,7 @@ def build_kube_config(profiles: list, config: ConfigManager, username: str, cli:
202191

203192
# merge any existing config with the new config
204193
# and write it to disk
205-
merge_new_with_existing(
206-
clusters=clusters,
207-
contexts=contexts,
208-
users=users,
209-
tenant=tenant,
210-
filename=filename
211-
)
194+
merge_new_with_existing(clusters=clusters, contexts=contexts, users=users, tenant=tenant, filename=filename)
212195

213196
# if required ensure we tell the user they need to modify their KUBECONFIG env var
214197
# in order to pick up the Britive managed kube config file

0 commit comments

Comments
 (0)