Skip to content

Commit 890206e

Browse files
authored
Merge pull request #1321 from UgnineSirdis/init-oauth2-credentials-from-config
Load OAuth 2.0 token exchange credentials provider from config file
2 parents c2b8607 + 49dd85e commit 890206e

File tree

6 files changed

+1208
-79
lines changed

6 files changed

+1208
-79
lines changed

CHANGELOG.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
* Added `query.ResultSet.Index()` method
2+
* Support loading OAuth 2.0 token exchange credentials provider from config file
3+
* Added options for JWT tokens for loading EC private keys and HMAC secrets
24

35
## v3.74.5
46
* Fixed bug with reading empty result set parts.
@@ -8,11 +10,11 @@
810
* Fixed bug with fail cast of grpc response to `operation.{Response,Status}`
911

1012
## v3.74.3
11-
* Removed check the node is available for query and table service sessions
13+
* Removed check the node is available for query and table service sessions
1214
* Refactored the `balancers.PreferLocations()` function - it is a clean/pure function
1315
* Added experimental `balancers.WithNodeID()` context modifier for define per request the YDB endpoint by NodeID
1416
* Reverted the allowing the casts from signed YDB types to unsigned destination types if source value is not negative
15-
* Replaced internal query session pool by default to stub for exclude impact from internal/pool
17+
* Replaced internal query session pool by default to stub for exclude impact from internal/pool
1618

1719
## v3.74.2
1820
* Added description to scan errors with use query service client scanner
@@ -61,7 +63,7 @@
6163
## v3.68.0
6264
* Added experimental `ydb.{Register,Unregister}DsnParser` global funcs for register/unregister external custom DSN parser for `ydb.Open` and `sql.Open` driver constructor
6365
* Simple implement option WithReaderWithoutConsumer
64-
* Fixed bug: topic didn't send specified partition number to a server
66+
* Fixed bug: topic didn't send specified partition number to a server
6567

6668
## v3.67.2
6769
* Fixed incorrect formatting of decimal. Implementation of decimal has been reverted to latest working version
@@ -86,12 +88,12 @@
8688
* Added Flush method for topic writer
8789

8890
## v3.66.0
89-
* Added experimental package `retry/budget` for limit second and subsequent retry attempts
91+
* Added experimental package `retry/budget` for limit second and subsequent retry attempts
9092
* Refactored internals for enabling `containedctx` linter
9193
* Fixed the hanging semaphore issue on coordination session reconnect
9294

9395
## v3.65.3
94-
* Fixed data race in `internal/conn.grpcClientStream`
96+
* Fixed data race in `internal/conn.grpcClientStream`
9597

9698
## v3.65.2
9799
* Fixed data race using `log.WithNames`

credentials/credentials.go

+51
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,57 @@ func NewOauth2TokenExchangeCredentials(
4343
return credentials.NewOauth2TokenExchangeCredentials(opts...)
4444
}
4545

46+
/*
47+
NewOauth2TokenExchangeCredentialsFile makes OAuth 2.0 token exchange protocol credentials object from config file
48+
https://www.rfc-editor.org/rfc/rfc8693
49+
Config file must be a valid json file
50+
51+
Fields of json file
52+
53+
grant-type: [string] Grant type option (default: "urn:ietf:params:oauth:grant-type:token-exchange")
54+
res: [string] Resource option (optional)
55+
aud: [string | list of strings] Audience option for token exchange request (optional)
56+
scope: [string | list of strings] Scope option (optional)
57+
requested-token-type: [string] Requested token type option (default: "urn:ietf:params:oauth:token-type:access_token")
58+
subject-credentials: [creds_json] Subject credentials options (optional)
59+
actor-credentials: [creds_json] Actor credentials options (optional)
60+
token-endpoint: [string] Token endpoint
61+
62+
Fields of creds_json (JWT):
63+
64+
type: [string] Token source type. Set JWT
65+
alg: [string] Algorithm for JWT signature.
66+
Supported algorithms can be listed
67+
with GetSupportedOauth2TokenExchangeJwtAlgorithms()
68+
private-key: [string] (Private) key in PEM format (RSA, EC) or Base64 format (HMAC) for JWT signature
69+
kid: [string] Key id JWT standard claim (optional)
70+
iss: [string] Issuer JWT standard claim (optional)
71+
sub: [string] Subject JWT standard claim (optional)
72+
aud: [string | list of strings] Audience JWT standard claim (optional)
73+
jti: [string] JWT ID JWT standard claim (optional)
74+
ttl: [string] Token TTL (default: 1h)
75+
76+
Fields of creds_json (FIXED):
77+
78+
type: [string] Token source type. Set FIXED
79+
token: [string] Token value
80+
token-type: [string] Token type value. It will become
81+
subject_token_type/actor_token_type parameter
82+
in token exchange request (https://www.rfc-editor.org/rfc/rfc8693)
83+
*/
84+
func NewOauth2TokenExchangeCredentialsFile(
85+
configFilePath string,
86+
opts ...credentials.Oauth2TokenExchangeCredentialsOption,
87+
) (Credentials, error) {
88+
return credentials.NewOauth2TokenExchangeCredentialsFile(configFilePath, opts...)
89+
}
90+
91+
// GetSupportedOauth2TokenExchangeJwtAlgorithms returns supported algorithms for
92+
// initializing OAuth 2.0 token exchange protocol credentials from config file
93+
func GetSupportedOauth2TokenExchangeJwtAlgorithms() []string {
94+
return credentials.GetSupportedOauth2TokenExchangeJwtAlgorithms()
95+
}
96+
4697
// NewJWTTokenSource makes JWT token source for OAuth 2.0 token exchange credentials
4798
func NewJWTTokenSource(opts ...credentials.JWTTokenSourceOption) (credentials.TokenSource, error) {
4899
return credentials.NewJWTTokenSource(opts...)

credentials/options.go

+46-3
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,19 @@ func WithTokenTTL(ttl time.Duration) credentials.JWTTokenSourceOption {
115115
return credentials.WithTokenTTL(ttl)
116116
}
117117

118+
// KeyID
119+
func WithKeyID(id string) credentials.JWTTokenSourceOption {
120+
return credentials.WithKeyID(id)
121+
}
122+
118123
// SigningMethod
119124
func WithSigningMethod(method jwt.SigningMethod) credentials.JWTTokenSourceOption {
120125
return credentials.WithSigningMethod(method)
121126
}
122127

123-
// KeyID
124-
func WithKeyID(id string) credentials.JWTTokenSourceOption {
125-
return credentials.WithKeyID(id)
128+
// SigningMethod
129+
func WithSigningMethodName(method string) credentials.JWTTokenSourceOption {
130+
return credentials.WithSigningMethodName(method)
126131
}
127132

128133
// PrivateKey
@@ -131,11 +136,49 @@ func WithPrivateKey(key interface{}) credentials.JWTTokenSourceOption {
131136
}
132137

133138
// PrivateKey
139+
// For RSA signing methods: RS256, RS384, RS512, PS256, PS384, PS512
134140
func WithRSAPrivateKeyPEMContent(key []byte) credentials.JWTTokenSourceOption {
135141
return credentials.WithRSAPrivateKeyPEMContent(key)
136142
}
137143

138144
// PrivateKey
145+
// For RSA signing methods: RS256, RS384, RS512, PS256, PS384, PS512
139146
func WithRSAPrivateKeyPEMFile(path string) credentials.JWTTokenSourceOption {
140147
return credentials.WithRSAPrivateKeyPEMFile(path)
141148
}
149+
150+
// PrivateKey
151+
// For EC signing methods: ES256, ES384, ES512
152+
func WithECPrivateKeyPEMContent(key []byte) credentials.JWTTokenSourceOption {
153+
return credentials.WithECPrivateKeyPEMContent(key)
154+
}
155+
156+
// PrivateKey
157+
// For EC signing methods: ES256, ES384, ES512
158+
func WithECPrivateKeyPEMFile(path string) credentials.JWTTokenSourceOption {
159+
return credentials.WithECPrivateKeyPEMFile(path)
160+
}
161+
162+
// Key
163+
// For HMAC signing methods: HS256, HS384, HS512
164+
func WithHMACSecretKey(key []byte) credentials.JWTTokenSourceOption {
165+
return credentials.WithHMACSecretKey(key)
166+
}
167+
168+
// Key
169+
// For HMAC signing methods: HS256, HS384, HS512
170+
func WithHMACSecretKeyBase64Content(base64KeyContent string) credentials.JWTTokenSourceOption {
171+
return credentials.WithHMACSecretKeyBase64Content(base64KeyContent)
172+
}
173+
174+
// Key
175+
// For HMAC signing methods: HS256, HS384, HS512
176+
func WithHMACSecretKeyFile(path string) credentials.JWTTokenSourceOption {
177+
return credentials.WithHMACSecretKeyFile(path)
178+
}
179+
180+
// Key
181+
// For HMAC signing methods: HS256, HS384, HS512
182+
func WithHMACSecretKeyBase64File(path string) credentials.JWTTokenSourceOption {
183+
return credentials.WithHMACSecretKeyBase64File(path)
184+
}

0 commit comments

Comments
 (0)