Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/auth #19

Merged
merged 2 commits into from
Jul 31, 2024
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
18 changes: 13 additions & 5 deletions arlas/cli/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ def list_configurations():
def create_configuration(
name: str = typer.Argument(help="Name of the configuration"),
server: str = typer.Option(help="ARLAS Server url"),
persistence: str = typer.Option(default=None, help="ARLAS Persistence url"),
headers: list[str] = typer.Option([], help="header (name:value)"),

persistence: str = typer.Option(default=None, help="ARLAS Persistence url"),
persistence_headers: list[str] = typer.Option([], help="header (name:value)"),

elastic: str = typer.Option(default=None, help="dictionary of name/es resources"),
elastic_login: str = typer.Option(default=None, help="elasticsearch login"),
elastic_password: str = typer.Option(default=None, help="elasticsearch password"),
elastic_headers: list[str] = typer.Option([], help="header (name:value)"),
allow_delete: bool = typer.Option(default=False, help="Is delete command allowed for this configuration?"),

auth_token_url: str = typer.Option(default=None, help="Token URL of the authentication service"),
auth_headers: list[str] = typer.Option([], help="header (name:value)"),
auth_org: str = typer.Option(default=None, help="ARLAS IAM Organization"),
auth_login: str = typer.Option(default=None, help="login"),
auth_password: str = typer.Option(default=None, help="password"),
auth_client_id: str = typer.Option(default=None, help="Client ID"),
Expand All @@ -42,17 +47,20 @@ def create_configuration(
print("Error: a configuration with that name already exists, please remove it first.", file=sys.stderr)
exit(1)

if auth_org:
headers.append("arlas-org-filter:" + auth_org)
auth_headers.append("arlas-org-filter:" + auth_org)
persistence_headers.append("arlas-org-filter:" + auth_org)

conf = ARLAS(
server=Resource(location=server, headers=dict(map(lambda h: (h.split(":")[0], h.split(":")[1]), headers))),
allow_delete=allow_delete)
if persistence:
conf.persistence = Resource(location=persistence, headers=dict(map(lambda h: (h.split(":")[0], h.split(":")[1]), headers)))
conf.persistence = Resource(location=persistence, headers=dict(map(lambda h: (h.split(":")[0], h.split(":")[1]), persistence_headers)))

if auth_token_url:
conf.authorization = AuthorizationService(
token_url=Resource(location=auth_token_url, headers=dict(map(lambda h: (h.split(":")[0], h.split(":")[1]), auth_headers))),
login=auth_login,
password=auth_password,
token_url=Resource(login=auth_login, password=auth_password, location=auth_token_url, headers=dict(map(lambda h: (h.split(":")[0], h.split(":")[1]), auth_headers))),
client_id=auth_client_id,
client_secret=auth_client_secret,
grant_type=auth_grant_type,
Expand Down
16 changes: 8 additions & 8 deletions arlas/cli/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from alive_progress import alive_bar
import requests

from arlas.cli.settings import ARLAS, Configuration, Resource
from arlas.cli.settings import ARLAS, Configuration, Resource, AuthorizationService


class RequestException(Exception):
Expand Down Expand Up @@ -235,7 +235,7 @@ def __get_fields__(origin: list[str], properties: dict[str:dict]):
return fields

def __arlas__(arlas: str, suffix, post=None, put=None, delete=None, service=Services.arlas_server):
configuration: ARLAS = Configuration.settings.arlas.get(arlas, {})
configuration: ARLAS = Configuration.settings.arlas.get(arlas, None)
if configuration is None:
print("Error: arlas configuration ({}) not found among [{}] for {}.".format(arlas, ", ".join(Configuration.settings.arlas.keys()), service.name), file=sys.stderr)
exit(1)
Expand Down Expand Up @@ -320,18 +320,18 @@ def __fetch__(resource: Resource, bytes: bool = False):
exit(1)

def __get_token__(arlas: str) -> str:
auth = Configuration.settings.arlas[arlas].authorization
if Configuration.settings.arlas[arlas].authorization.arlas_iam:
auth: AuthorizationService = Configuration.settings.arlas[arlas].authorization
if auth.arlas_iam:
data = {
"email": auth.login,
"password": auth.password
"email": auth.token_url.login,
"password": auth.token_url.password
}
else:
data = {
"client_id": auth.client_id,
"client_secret": auth.client_secret,
"username": auth.login,
"password": auth.password
"username": auth.token_url.login,
"password": auth.token_url.password
}
if auth.grant_type:
data["grant_type"] = auth.grant_type
Expand Down
2 changes: 0 additions & 2 deletions arlas/cli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Resource(BaseModel):

class AuthorizationService(BaseModel):
token_url: Resource = Field(default=None, title="Token URL of the authentication service")
login: str = Field(default=None, title="login")
password: str = Field(default=None, title="password")
client_id: str | None = Field(default=None, title="Client ID")
client_secret: str | None = Field(default=None, title="Client secret")
grant_type: str | None = Field(default=None, title="Grant type (e.g. password)")
Expand Down
56 changes: 56 additions & 0 deletions docs/docs/arlas_cloud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
To configure `arlas_cli` for your cloud.arlas.io account:

First, set the following environment variables by changing appropriately `SET_THIS_VALUE`:
```shell
export MY_ORGANIZATION=SET_THIS_VALUE
export ARLAS_USER=SET_THIS_VALUE
export ARLAS_PWD=SET_THIS_VALUE
export ELASTIC_ENDPOINT=SET_THIS_VALUE
export ELASTIC_USER=SET_THIS_VALUE
export ELASTIC_PWD=SET_THIS_VALUE
```

<!-- termynal -->
```shell
> arlas_cli confs \
create cloud.arlas.io \
--server "https://cloud.arlas.io/arlas/server" \
--headers "arlas-org-filter:${MY_ORGANIZATION}" \
--headers "Content-Type:application/json" \
--no-allow-delete \
--auth-token-url https://cloud.arlas.io/arlas/iam/session \
--auth-login "${ARLAS_USER}" \
--auth-password "${ARLAS_PWD}" \
--auth-headers "Content-Type:application/json;charset=utf-8" \
--auth-org "${MY_ORGANIZATION}" \
--elastic "${ELASTIC_ENDPOINT}" \
--elastic-headers "Content-Type:application/json" \
--elastic-login "${ELASTIC_USER}" \
--elastic-password "${ELASTIC_PWD}" \
--elastic-headers "Content-Type:application/json" \
--auth-headers "Content-Type:application/json;charset=utf-8" \
--persistence "https://cloud.arlas.io/arlas/persistence" \
--persistence-headers "Content-Type:application/json" \
--auth-arlas-iam
```

Check the configuration exists:
<!-- termynal -->
```shell
> arlas_cli confs list
```

You can now try the configuration:
<!-- termynal -->
```shell
> arlas_cli collections --config cloud.arlas.io list
```

<!-- termynal -->
```shell
> arlas_cli indices --config cloud.arlas.io list
```
<!-- termynal -->
```shell
> arlas_cli persist --config cloud.arlas.io groups config.json
```
8 changes: 5 additions & 3 deletions docs/docs/install.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

## Prerequisite

- python 3.11
- python 3.10
- pip
- elasticsearch

If you manage your own ARLAS stack, you will also need:
- [ARLAS](https://github.com/gisaia/ARLAS-Exploration-stack)

## Install
Expand All @@ -18,5 +19,6 @@ In a new terminal, you should be able to run it:
<!-- termynal -->
```shell
> arlas_cli --version
0.2.8
X.X.X
```

4 changes: 2 additions & 2 deletions docs/docs/started.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Install `arlas_cli` ([prerequisite](install.md#Prerequisite))
```

!!! warning "Prerequisite"
For running the various examples bellow, ARLAS and elasticsearch must be running on the local machine.
For running the various examples bellow, ARLAS and elasticsearch must be running on the local machine: clone the [ARLAS Stack Exploration](https://github.com/gisaia/ARLAS-Exploration-stack) project and run `./start.sh` .

## Initial configuration
`arlas_cli` uses a yaml file for storing various ARLAS and elasticsearch configurations. By default, the file is located in `~/.arlas/cli/configuration.yaml`. [One is automatically created for your convenience at the first launch](https://raw.githubusercontent.com/gisaia/arlas-cli/master/configuration.yaml). It contains the ARLAS demo endpoint and the local ARLAS and elasticsearch endpoints.
Expand All @@ -18,7 +18,7 @@ It can also contain references to index mappings for creating indices. A default
<!-- termynal -->
```shell
> arlas_cli --version
0.2.8
X.X.X
Warning : no configuration file found, we created an empty one for
you (~/.arlas/cli/configuration.yaml).
```
Expand Down
2 changes: 1 addition & 1 deletion docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ nav:
- Persistence: persist.md
- Configurations: confs.md
- Configuration data model: model/model.md

- Configuring ARLAS Cloud: arlas_cloud.md

plugins:
- termynal:
Expand Down
Loading