rdeploy 0.5.0__tar.gz → 0.5.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rdeploy
3
- Version: 0.5.0
3
+ Version: 0.5.2
4
4
  Summary: CLI that makes it easy to build and deploy an application to k8s.
5
5
  Author-email: Rehive <info@rehive.com>
6
6
  License: MIT
@@ -167,6 +167,10 @@ kubectl fails with an authentication error such as `invalid_grant` or
167
167
  rdeploy kubectl-login
168
168
  ```
169
169
 
170
+ If gcloud's own login has expired too, rdeploy signs gcloud in first: gcloud
171
+ asks for your password in the terminal, or opens the browser for
172
+ `gcloud auth login`.
173
+
170
174
  To check whether the saved session is still valid (exits non-zero if not):
171
175
 
172
176
  ```bash
@@ -130,6 +130,10 @@ kubectl fails with an authentication error such as `invalid_grant` or
130
130
  rdeploy kubectl-login
131
131
  ```
132
132
 
133
+ If gcloud's own login has expired too, rdeploy signs gcloud in first: gcloud
134
+ asks for your password in the terminal, or opens the browser for
135
+ `gcloud auth login`.
136
+
133
137
  To check whether the saved session is still valid (exits non-zero if not):
134
138
 
135
139
  ```bash
rdeploy-0.5.2/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.2
@@ -170,6 +170,41 @@ def _installed_client(data, source):
170
170
  return client
171
171
 
172
172
 
173
+ def run_gcloud(cmd):
174
+ """
175
+ Run a gcloud command, capturing only its output. Its prompts and errors go
176
+ to the terminal, so gcloud can ask for a password when the login needs
177
+ reauthentication; with stderr captured it refuses to prompt.
178
+ """
179
+ return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)
180
+
181
+
182
+ def ensure_gcloud_login(account=None):
183
+ """
184
+ Make sure gcloud can get a token for `account` (default: the active
185
+ account) before rdeploy uses it, signing gcloud in again if needed.
186
+
187
+ A password or security-key reauthentication is left to gcloud, which
188
+ prompts for it in the terminal. When gcloud's login has expired, or there
189
+ is none, run `gcloud auth login`, which signs in through the browser.
190
+ """
191
+ account_flag = [f'--account={account}'] if account else []
192
+ # The token itself is never needed: it goes to /dev/null, so rdeploy never
193
+ # holds it. Only the exit code and gcloud's error matter.
194
+ token_cmd = ['gcloud', 'auth', 'print-access-token', *account_flag]
195
+ check = subprocess.run(token_cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
196
+ stderr=subprocess.PIPE, text=True)
197
+ if check.returncode == 0:
198
+ return
199
+ if ('reauth' in check.stderr.lower()
200
+ and subprocess.run(token_cmd, stdout=subprocess.DEVNULL).returncode == 0):
201
+ return
202
+ print('gcloud needs you to sign in again. Opening your browser for gcloud first.', flush=True)
203
+ login_cmd = ['gcloud', 'auth', 'login'] + ([account] if account else [])
204
+ if subprocess.run(login_cmd).returncode != 0:
205
+ raise KubectlLoginError('gcloud sign-in failed; gcloud\'s error is above.')
206
+
207
+
173
208
  def load_client(project, secret, account=None, client_json=None):
174
209
  """Read the OAuth client from a local file, or from Secret Manager with the user's gcloud login."""
175
210
  if client_json:
@@ -180,12 +215,12 @@ def load_client(project, secret, account=None, client_json=None):
180
215
  f'--secret={secret}', f'--project={project}']
181
216
  if account:
182
217
  cmd.append(f'--account={account}')
183
- result = subprocess.run(cmd, capture_output=True, text=True)
218
+ result = run_gcloud(cmd)
184
219
  if result.returncode != 0:
185
220
  raise KubectlLoginError(
186
221
  'Couldn\'t read the OAuth client from Secret Manager'
187
- f' (secret {secret} in project {project}):\n{result.stderr.strip()}\n'
188
- 'Check that gcloud is signed in with your work account and uses your'
222
+ f' (secret {secret} in project {project}); gcloud\'s error is above.'
223
+ ' Check that gcloud is signed in with your work account and uses your'
189
224
  ' device certificate (`gcloud auth list`).')
190
225
  try:
191
226
  data = json.loads(result.stdout)
@@ -370,10 +405,10 @@ def list_clusters(project, account=None):
370
405
  '--format=json(name,location)']
371
406
  if account:
372
407
  cmd.append(f'--account={account}')
373
- result = subprocess.run(cmd, capture_output=True, text=True)
408
+ result = run_gcloud(cmd)
374
409
  if result.returncode != 0:
375
410
  raise KubectlLoginError(
376
- f'Couldn\'t list the GKE clusters in {project}:\n{result.stderr.strip()}')
411
+ f'Couldn\'t list the GKE clusters in {project}; gcloud\'s error is above.')
377
412
  return [(c['name'], c['location']) for c in json.loads(result.stdout or '[]')]
378
413
 
379
414
 
@@ -408,10 +443,10 @@ def get_credentials(project, location, name, account=None):
408
443
  if account:
409
444
  cmd.append(f'--account={account}')
410
445
  print('Adding {} to your kubeconfig'.format(name), flush=True)
411
- result = subprocess.run(cmd, capture_output=True, text=True)
446
+ result = run_gcloud(cmd)
412
447
  if result.returncode != 0:
413
448
  raise KubectlLoginError(
414
- f'Couldn\'t add cluster {name} to your kubeconfig:\n{result.stderr.strip()}')
449
+ f'Couldn\'t add cluster {name} to your kubeconfig; gcloud\'s error is above.')
415
450
 
416
451
 
417
452
  def configure_kubeconfig(project, creds_path, account=None, only_clusters=()):
@@ -471,6 +506,10 @@ def login(project=None, secret=None, account=None, clusters=None, client_json=No
471
506
  check_tools(need_kubeconfig=not skip_kubeconfig)
472
507
  settings = resolve_settings(project, secret, account, clusters)
473
508
 
509
+ # gcloud reads the OAuth client and lists the clusters.
510
+ if not (client_json and skip_kubeconfig):
511
+ ensure_gcloud_login(settings['account'])
512
+
474
513
  client = load_client(settings['project'], settings['secret'], settings['account'], client_json)
475
514
  token = sign_in(client, settings['account'] or gcloud_account(), open_browser)
476
515
  path = write_credentials(client, token['refresh_token'])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rdeploy
3
- Version: 0.5.0
3
+ Version: 0.5.2
4
4
  Summary: CLI that makes it easy to build and deploy an application to k8s.
5
5
  Author-email: Rehive <info@rehive.com>
6
6
  License: MIT
@@ -167,6 +167,10 @@ kubectl fails with an authentication error such as `invalid_grant` or
167
167
  rdeploy kubectl-login
168
168
  ```
169
169
 
170
+ If gcloud's own login has expired too, rdeploy signs gcloud in first: gcloud
171
+ asks for your password in the terminal, or opens the browser for
172
+ `gcloud auth login`.
173
+
170
174
  To check whether the saved session is still valid (exits non-zero if not):
171
175
 
172
176
  ```bash
@@ -1,6 +1,7 @@
1
1
  import json
2
2
  import os
3
3
  import stat
4
+ import subprocess
4
5
  import threading
5
6
  import urllib.error
6
7
  import urllib.parse
@@ -126,10 +127,21 @@ class TestLoadClient:
126
127
 
127
128
  @patch('rdeploy.gke_auth.subprocess.run')
128
129
  def test_secret_manager_error(self, mock_run):
129
- mock_run.return_value = completed(returncode=1, stderr='PERMISSION_DENIED')
130
- with pytest.raises(KubectlLoginError, match='PERMISSION_DENIED'):
130
+ mock_run.return_value = completed(returncode=1)
131
+ with pytest.raises(KubectlLoginError, match='secret s in project p'):
131
132
  gke_auth.load_client('p', 's')
132
133
 
134
+ @patch('rdeploy.gke_auth.subprocess.run')
135
+ def test_gcloud_can_prompt_for_reauth(self, mock_run):
136
+ # gcloud only prompts for a password when stdin and stderr are the
137
+ # terminal, so neither may be captured.
138
+ mock_run.return_value = completed(json.dumps({'installed': CLIENT}))
139
+ gke_auth.load_client('p', 's')
140
+ kwargs = mock_run.call_args[1]
141
+ assert kwargs['stdout'] == subprocess.PIPE
142
+ assert 'stderr' not in kwargs and 'stdin' not in kwargs
143
+ assert 'capture_output' not in kwargs
144
+
133
145
 
134
146
  # Sign-in
135
147
  #########
@@ -358,6 +370,27 @@ class TestLogin:
358
370
  def in_tmp_dir(self, tmp_path, monkeypatch):
359
371
  monkeypatch.chdir(tmp_path)
360
372
 
373
+ @pytest.fixture(autouse=True)
374
+ def gcloud_signed_in(self):
375
+ with patch('rdeploy.gke_auth.ensure_gcloud_login') as ensure:
376
+ yield ensure
377
+
378
+ def test_checks_gcloud_login_first(self, gcloud_signed_in):
379
+ with patch('rdeploy.gke_auth.check_tools'), \
380
+ patch('rdeploy.gke_auth.load_client', return_value=CLIENT), \
381
+ patch('rdeploy.gke_auth.sign_in', return_value={'refresh_token': 'r'}), \
382
+ patch('rdeploy.gke_auth.configure_kubeconfig', return_value=['ctx']):
383
+ gke_auth.login(project='p', secret='s', account='me@example.test')
384
+ gcloud_signed_in.assert_called_once_with('me@example.test')
385
+
386
+ def test_no_gcloud_needed_with_local_client_and_no_kubeconfig(self, gcloud_signed_in, tmp_path):
387
+ path = tmp_path / 'client.json'
388
+ path.write_text(json.dumps({'installed': CLIENT}))
389
+ with patch('rdeploy.gke_auth.check_tools'), \
390
+ patch('rdeploy.gke_auth.sign_in', return_value={'refresh_token': 'r'}):
391
+ gke_auth.login(project='p', secret='s', client_json=str(path), skip_kubeconfig=True)
392
+ gcloud_signed_in.assert_not_called()
393
+
361
394
  @patch('rdeploy.gke_auth.configure_kubeconfig', return_value=['ctx'])
362
395
  @patch('rdeploy.gke_auth.sign_in', return_value={'refresh_token': 'r'})
363
396
  @patch('rdeploy.gke_auth.load_client', return_value=CLIENT)
@@ -385,3 +418,60 @@ class TestLogin:
385
418
  raise KubectlLoginError('explained')
386
419
  with pytest.raises(SystemExit, match='explained'):
387
420
  gke_auth.run(fail)
421
+
422
+
423
+ # gcloud login
424
+ ##############
425
+
426
+ class TestEnsureGcloudLogin:
427
+ TOKEN = ['gcloud', 'auth', 'print-access-token', '--account=me@example.test']
428
+
429
+ @patch('rdeploy.gke_auth.subprocess.run')
430
+ def test_signed_in(self, mock_run):
431
+ mock_run.return_value = completed('token')
432
+ gke_auth.ensure_gcloud_login('me@example.test')
433
+ assert mock_run.call_count == 1
434
+ assert mock_run.call_args[0][0] == self.TOKEN
435
+ kwargs = mock_run.call_args[1]
436
+ assert kwargs['stdin'] == subprocess.DEVNULL
437
+ assert kwargs['stdout'] == subprocess.DEVNULL # the token is discarded
438
+ assert kwargs['stderr'] == subprocess.PIPE
439
+
440
+ @patch('rdeploy.gke_auth.subprocess.run')
441
+ def test_reauth_prompts_in_the_terminal(self, mock_run):
442
+ mock_run.side_effect = [
443
+ completed(returncode=1, stderr='Reauthentication failed. cannot prompt during non-interactive execution.'),
444
+ completed('token'),
445
+ ]
446
+ gke_auth.ensure_gcloud_login('me@example.test')
447
+ retry = mock_run.call_args_list[1]
448
+ assert retry[0][0] == self.TOKEN
449
+ assert 'stderr' not in retry[1] and 'stdin' not in retry[1]
450
+ assert retry[1]['stdout'] == subprocess.DEVNULL
451
+
452
+ @patch('rdeploy.gke_auth.subprocess.run')
453
+ def test_expired_login_runs_gcloud_auth_login(self, mock_run, capsys):
454
+ mock_run.side_effect = [
455
+ completed(returncode=1, stderr='There was a problem refreshing your current auth tokens. Please run: gcloud auth login'),
456
+ completed(),
457
+ ]
458
+ gke_auth.ensure_gcloud_login('me@example.test')
459
+ assert mock_run.call_args_list[1][0][0] == ['gcloud', 'auth', 'login', 'me@example.test']
460
+ assert 'sign in again' in capsys.readouterr().out
461
+
462
+ @patch('rdeploy.gke_auth.subprocess.run')
463
+ def test_failed_reauth_falls_back_to_login(self, mock_run):
464
+ mock_run.side_effect = [
465
+ completed(returncode=1, stderr='reauth related error (invalid_rapt)'),
466
+ completed(returncode=1),
467
+ completed(),
468
+ ]
469
+ gke_auth.ensure_gcloud_login()
470
+ assert mock_run.call_args_list[0][0][0] == ['gcloud', 'auth', 'print-access-token']
471
+ assert mock_run.call_args_list[2][0][0] == ['gcloud', 'auth', 'login']
472
+
473
+ @patch('rdeploy.gke_auth.subprocess.run')
474
+ def test_login_fails(self, mock_run):
475
+ mock_run.side_effect = [completed(returncode=1, stderr='no credentialed accounts'), completed(returncode=1)]
476
+ with pytest.raises(KubectlLoginError, match='gcloud sign-in failed'):
477
+ gke_auth.ensure_gcloud_login()
rdeploy-0.5.0/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.5.0
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes