Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ This will prompt for a password. It is also possible to use the `--password` arg
to avoid the prompt, though that is a less secure option as it can expose the password
through command history and process listings.


### With API keys

It's possible to use the CLI with API keys. In this case the `login` step is
not run, but the API key is set in the environment via `STACKLET_API_KEY`.

This can be done once via `export STACKLET_API_KEY=<my-key>` or by passing it before every command, like:

```
STACKLET_API_KEY=<my-key> stacklet-admin <command> <subcommand>
```


## Running Commands

Commands are grouped into command groups, for example, all the commands relating to accounts can be
Expand Down
8 changes: 5 additions & 3 deletions stacklet/client/platform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging
import os
from pathlib import Path

import click

Expand Down Expand Up @@ -91,9 +92,10 @@ def wrap_command(func, options, required=False, prompt=False):


def get_token():
with open(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS), "r") as f:
token = f.read()
return token
if token := os.getenv("STACKLET_API_KEY"):
return token

return Path(StackletContext.DEFAULT_CREDENTIALS).expanduser().read_text()


def default_options(*args, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ def test_get_token(self, patched_context):
token = get_token()
self.assertEqual(token, "foo")

def test_get_token_env(self):
token = "foo"
with patch.dict(os.environ, {"STACKLET_API_KEY": token}):
self.assertEqual(get_token(), "foo")

@patch("stacklet.client.platform.utils.StackletContext")
def test_get_token_env_precedence(self, patched_context):
patched_context.DEFAULT_CREDENTIALS = self.credential_file.name
token = "another"
with patch.dict(os.environ, {"STACKLET_API_KEY": token}):
self.assertEqual(get_token(), "another")

def test_get_log_level(self):
self.assertEqual(get_log_level(-9999), 50)
self.assertEqual(get_log_level(0), 40)
Expand Down