Skip to content

Commit fb5bcd1

Browse files
authored
Improve authentication documentation (#1302)
* fix: improve the authentication documentation * fix: improve structure * fix: address PR comments
1 parent 42d1460 commit fb5bcd1

File tree

1 file changed

+88
-52
lines changed

1 file changed

+88
-52
lines changed

README.md

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -99,90 +99,126 @@ 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`). You can create a service account through the STACKIT Portal.
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-
```
123-
124-
Check the [authentication example](examples/authentication/authentication.go) for more details.
114+
2. **Token Flow**
115+
- Uses long-lived service account tokens
116+
- Simpler but less secure
125117

126-
### Key flow
118+
### Configuration Priority
127119

128-
The following instructions assume that you have created a service account and assigned it the necessary permissions, e.g. `project.owner`.
120+
The SDK searches for credentials in the following order:
129121

130-
To use the key flow, you need to have a service account key, which must have an RSA key-pair attached to it.
122+
1. Explicit configuration in code
123+
2. Environment variables
124+
3. Credentials file (`$HOME/.stackit/credentials.json`)
131125

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.
126+
For each authentication method, the key flow is attempted first, followed by the token flow.
133127

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).
128+
### Using the Key Flow
135129

136-
To configure the key flow, follow this steps:
130+
1. Create a service account key in the STACKIT Portal:
137131

138-
1. Create a service account key:
132+
- Navigate to `Service Accounts` → Select account → `Service Account Keys` → Create key
133+
- You can either let STACKIT generate the key pair or provide your own RSA key pair (see [Creating an RSA key-pair](https://docs.stackit.cloud/stackit/en/usage-of-the-service-account-keys-in-stackit-175112464.html#UsageoftheserviceaccountkeysinSTACKIT-CreatinganRSAkey-pair) for more details)
134+
- **Note**: it's also possible to create the service account key in other ways (see [Tutorials for Service Accounts](https://docs.stackit.cloud/stackit/en/tutorials-for-service-accounts-134415861.html) for more details)
139135

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)
141-
142-
2. Save the content of the service account key by copying it and saving it in a JSON file.
143-
144-
The expected format of the service account key is a **json** with the following structure:
136+
2. Save the service account key JSON:
145137

146138
```json
147139
{
148140
"id": "uuid",
149141
"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,
156142
"credentials": {
157143
"kid": "string",
158144
159145
"sub": "uuid",
160146
"aud": "string",
161-
(optional) "privateKey": "private key when generated by the SA service"
147+
"privateKey": "private key (if STACKIT-generated)"
162148
}
149+
// ... other fields ...
163150
}
164151
```
165152

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)
153+
3. Configure authentication using any of these methods:
170154

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)
155+
**A. Code Configuration**
176156

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.
157+
```go
158+
// Using service account key file
159+
config.WithServiceAccountKeyPath("path/to/sa_key.json")
160+
// Or using key content directly
161+
config.WithServiceAccountKey(keyJSON)
162+
163+
// Optional: For custom key pairs
164+
config.WithPrivateKeyPath("path/to/private.pem")
165+
// Or using private key content directly
166+
config.WithPrivateKey(privateKeyJSON)
167+
```
178168

179-
### Token flow
169+
**B. Environment Variables**
180170

181-
Using this flow is less secure since the token is long-lived. You can provide the token in several ways:
171+
```bash
172+
# Using service account key
173+
STACKIT_SERVICE_ACCOUNT_KEY_PATH=/path/to/sa_key.json
174+
# or
175+
STACKIT_SERVICE_ACCOUNT_KEY=<sa-key-content>
176+
177+
# Optional: For custom key pairs
178+
STACKIT_PRIVATE_KEY_PATH=/path/to/private.pem
179+
# or
180+
STACKIT_PRIVATE_KEY=<private-key-content>
181+
```
182+
183+
**C. Credentials File** (`$HOME/.stackit/credentials.json`)
184+
185+
```json
186+
{
187+
"STACKIT_SERVICE_ACCOUNT_KEY_PATH": "/path/to/sa_key.json",
188+
"STACKIT_PRIVATE_KEY_PATH": "/path/to/private.pem"
189+
}
190+
```
191+
192+
### Using the Token Flow
193+
194+
1. Create an access token in the STACKIT Portal:
195+
196+
- Navigate to `Service Accounts` → Select account → `Access Tokens` → Create token
197+
- **Note**: it's also possible to create the service account access tokens in other ways (see [Tutorials for Service Accounts](https://docs.stackit.cloud/stackit/en/tutorials-for-service-accounts-134415861.html) for more details)
198+
199+
2. Configure authentication using any of these methods:
200+
201+
**A. Code Configuration**
202+
203+
```go
204+
config.WithToken("your-token")
205+
```
206+
207+
**B. Environment Variables**
208+
209+
```bash
210+
STACKIT_SERVICE_ACCOUNT_TOKEN=your-token
211+
```
212+
213+
**C. Credentials File** (`$HOME/.stackit/credentials.json`)
214+
215+
```json
216+
{
217+
"STACKIT_SERVICE_ACCOUNT_TOKEN": "your-token"
218+
}
219+
```
182220

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)
221+
For detailed implementation examples, see the [authentication example](examples/authentication/authentication.go).
186222

187223
## Reporting issues
188224

0 commit comments

Comments
 (0)