You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56-61Lines changed: 56 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,90 +99,85 @@ More examples on other services, configuration and authentication possibilities
99
99
100
100
## Authentication
101
101
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.
103
103
104
-
- Key flow (recommended)
105
-
- Token flow
104
+
### Authentication Methods
106
105
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:
108
107
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)
112
109
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
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**
133
130
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).
- 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**
141
150
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
143
156
144
-
The expected format of the service account key is a **json** with the following structure:
(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
165
165
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
> **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
176
171
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:
178
173
179
-
### Token flow
174
+
1. Explicit configuration in code
175
+
2. Environment variables
176
+
3. Credentials file
180
177
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.
182
179
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.
0 commit comments