Skip to content

Commit acd62ee

Browse files
committed
fix: improve the authentication documentation
1 parent e9196dc commit acd62ee

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

README.md

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -99,90 +99,85 @@ More examples on other services, configuration and authentication possibilities
9999

100100
## Authentication
101101

102-
To authenticate to the SDK, you will need a [service account](https://docs.stackit.cloud/stackit/en/service-accounts-134415819.html). Create it in the STACKIT Portal an assign it the necessary permissions, e.g. `project.owner`. There are multiple ways to authenticate:
102+
To authenticate with the SDK, you need a [service account](https://docs.stackit.cloud/stackit/en/service-accounts-134415819.html) with appropriate permissions (e.g., `project.owner`). This service account can for example be created in the Portal, please check the documentation for further details.
103103

104-
- Key flow (recommended)
105-
- Token flow
104+
### Authentication Methods
106105

107-
When setting up authentication, the SDK will always try to use the key flow first and search for credentials in several locations, following a specific order:
106+
The SDK supports two authentication methods:
108107

109-
1. Explicit configuration, e.g. by using the option `config.WithServiceAccountKeyPath("path/to/sa_key.json")`
110-
2. Environment variable, e.g. by setting `STACKIT_SERVICE_ACCOUNT_KEY_PATH`
111-
3. Credentials file
108+
1. **Key Flow** (Recommended)
112109

113-
The SDK will check the credentials file located in the path defined by the `STACKIT_CREDENTIALS_PATH` env var, if specified,
114-
or in `$HOME/.stackit/credentials.json` as a fallback.
115-
The credentials file should be a json and each credential should be set using the name of the respective environment variable, as stated below in each flow. Example:
110+
- Uses RSA key-pair based authentication
111+
- Provides better security through short-lived tokens
112+
- Supports both STACKIT-generated and custom key pairs
116113

117-
```json
118-
{
119-
"STACKIT_SERVICE_ACCOUNT_TOKEN": "foo_token",
120-
"STACKIT_SERVICE_ACCOUNT_KEY_PATH": "path/to/sa_key.json"
121-
}
122-
```
114+
2. **Token Flow**
115+
- Uses long-lived service account tokens
116+
- Simpler but less secure
123117

124-
Check the [authentication example](examples/authentication/authentication.go) for more details.
118+
### Configuration Options
125119

126-
### Key flow
120+
You can configure authentication using any of these methods:
127121

128-
The following instructions assume that you have created a service account and assigned it the necessary permissions, e.g. `project.owner`.
122+
1. **Explicit Configuration in Code**
129123

130-
To use the key flow, you need to have a service account key, which must have an RSA key-pair attached to it.
124+
```go
125+
config.WithServiceAccountKeyPath("path/to/sa_key.json")
126+
config.WithToken("your-token")
127+
```
131128

132-
When creating the service account key, a new pair can be created automatically, which will be included in the service account key. This will make it much easier to configure the key flow authentication in the CLI, by just providing the service account key.
129+
2. **Environment Variables**
133130

134-
**Optionally**, you can provide your own private key when creating the service account key, which will then require you to also provide it explicitly to the CLI, additionaly to the service account key. Check the STACKIT Knowledge Base for an [example of how to create your own key-pair](https://docs.stackit.cloud/stackit/en/usage-of-the-service-account-keys-in-stackit-175112464.html#UsageoftheserviceaccountkeysinSTACKIT-CreatinganRSAkey-pair).
131+
```bash
132+
STACKIT_SERVICE_ACCOUNT_KEY_PATH=/path/to/sa_key.json
133+
STACKIT_SERVICE_ACCOUNT_TOKEN=your-token
134+
```
135135

136-
To configure the key flow, follow this steps:
136+
3. **Credentials File**
137+
- Default location: `$HOME/.stackit/credentials.json`
138+
- Custom location: Set via `STACKIT_CREDENTIALS_PATH`
139+
- Format:
140+
```json
141+
{
142+
"STACKIT_SERVICE_ACCOUNT_KEY_PATH": "path/to/sa_key.json",
143+
"STACKIT_SERVICE_ACCOUNT_TOKEN": "your-token"
144+
}
145+
```
137146

138-
1. Create a service account key:
147+
### Configuration Reference
139148

140-
- Use the STACKIT Portal: go to the `Service Accounts` tab, choose a `Service Account` and go to `Service Account Keys` to create a key. For more details, see [Create a service account key](https://docs.stackit.cloud/stackit/en/create-a-service-account-key-175112456.html)
149+
1. **Explicit Configuration**
141150

142-
2. Save the content of the service account key by copying it and saving it in a JSON file.
151+
- `config.WithServiceAccountKey(string)` - Set the service account key JSON directly
152+
- `config.WithServiceAccountKeyPath(string)` - Set the path to the service account key JSON file
153+
- `config.WithPrivateKey(string)` - Set the service account private key directly (for custom key pairs)
154+
- `config.WithPrivateKeyPath(string)` - Set the path to the service account private key file
155+
- `config.WithToken(string)` - Set the service account access token directly
143156

144-
The expected format of the service account key is a **json** with the following structure:
157+
2. **Environment Variables**
145158

146-
```json
147-
{
148-
"id": "uuid",
149-
"publicKey": "public key",
150-
"createdAt": "2023-08-24T14:15:22Z",
151-
"validUntil": "2023-08-24T14:15:22Z",
152-
"keyType": "USER_MANAGED",
153-
"keyOrigin": "USER_PROVIDED",
154-
"keyAlgorithm": "RSA_2048",
155-
"active": true,
156-
"credentials": {
157-
"kid": "string",
158-
159-
"sub": "uuid",
160-
"aud": "string",
161-
(optional) "privateKey": "private key when generated by the SA service"
162-
}
163-
}
164-
```
159+
- `STACKIT_SERVICE_ACCOUNT_KEY` - Service account key JSON as string
160+
- `STACKIT_SERVICE_ACCOUNT_KEY_PATH` - Path to service account key JSON file
161+
- `STACKIT_PRIVATE_KEY` - Service account private key as string
162+
- `STACKIT_PRIVATE_KEY_PATH` - Path to service account private key file
163+
- `STACKIT_SERVICE_ACCOUNT_TOKEN` - Service account access token
164+
- `STACKIT_CREDENTIALS_PATH` - Custom path to credentials file
165165

166-
3. Configure the service account key for authentication in the SDK by following one of the alternatives below:
167-
- using the configuration options: `config.WithServiceAccountKey` or `config.WithServiceAccountKeyPath`, `config.WithPrivateKey` or `config.WithPrivateKeyPath`
168-
- setting the environment variable: `STACKIT_SERVICE_ACCOUNT_KEY_PATH`
169-
- setting `STACKIT_SERVICE_ACCOUNT_KEY_PATH` in the credentials file (see above)
166+
3. **Credentials File**
167+
- JSON file containing any of the above environment variable names as keys
168+
- Default location: `$HOME/.stackit/credentials.json`
170169

171-
> **Optionally, only if you have provided your own RSA key-pair when creating the service account key**, you also need to configure your private key (takes precedence over the one included in the service account key, if present). **The private key must be PEM encoded** and can be provided using one of the options below:
172-
>
173-
> - using the configuration options: `config.WithPrivateKey` or `config.WithPrivateKeyPath`
174-
> - setting the environment variable: `STACKIT_PRIVATE_KEY_PATH`
175-
> - setting `STACKIT_PRIVATE_KEY_PATH` in the credentials file (see above)
170+
### Configuration Priority
176171

177-
4. The SDK will search for the keys and, if valid, will use them to get access and refresh tokens which will be used to authenticate all the requests.
172+
The SDK searches for credentials in the following order:
178173

179-
### Token flow
174+
1. Explicit configuration in code
175+
2. Environment variables
176+
3. Credentials file
180177

181-
Using this flow is less secure since the token is long-lived. You can provide the token in several ways:
178+
For each authentication method, the key flow is attempted first, followed by the token flow.
182179

183-
1. Using the configuration option `config.WithToken`
184-
2. Setting the environment variable `STACKIT_SERVICE_ACCOUNT_TOKEN`
185-
3. Setting it in the credentials file (see above)
180+
Check the [authentication example](examples/authentication/authentication.go) for implementation details.
186181

187182
## Reporting issues
188183

0 commit comments

Comments
 (0)