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

Support OAuth login transformations #397

Merged
merged 5 commits into from
Nov 25, 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
9 changes: 9 additions & 0 deletions api/v1/ytsaurus_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ type OauthUserInfoHandlerSpec struct {
//+kubebuilder:default:=nickname
LoginField string `json:"loginField,omitempty"`
ErrorField *string `json:"errorField,omitempty"`
// LoginTransformations will be applied to the login field consequentially if set.
// Result of the transformations is treated as YTsaurus OAuth user's username.
LoginTransformations []OauthUserLoginTransformation `json:"loginTransformations,omitempty"`
}

type OauthUserLoginTransformation struct {
// MatchPattern expects RE2 (https://github.com/google/re2/wiki/syntax) syntax.
MatchPattern string `json:"matchPattern,omitempty"`
Replacement string `json:"replacement,omitempty"`
}

type OauthServiceSpec struct {
Expand Down
20 changes: 20 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions config/crd/bases/cluster.ytsaurus.tech_ytsaurus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14912,6 +14912,19 @@ spec:
loginField:
default: nickname
type: string
loginTransformations:
description: LoginTransformations will be applied to the login
field consequentially if set.
items:
properties:
matchPattern:
description: MatchPattern expects RE2 (https://github.com/google/re2/wiki/syntax)
syntax.
type: string
replacement:
type: string
type: object
type: array
type: object
type: object
primaryMasters:
Expand Down
23 changes: 22 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,24 @@ _Appears in:_
| `endpoint` _string_ | | user/info | |
| `loginField` _string_ | | nickname | |
| `errorField` _string_ | | | |
| `loginTransformations` _[OauthUserLoginTransformation](#oauthuserlogintransformation) array_ | LoginTransformations will be applied to the login field consequentially if set.<br />Result of the transformations is treated as YTsaurus OAuth user's username. | | |


#### OauthUserLoginTransformation







_Appears in:_
- [OauthUserInfoHandlerSpec](#oauthuserinfohandlerspec)

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `matchPattern` _string_ | MatchPattern expects RE2 (https://github.com/google/re2/wiki/syntax) syntax. | | |
| `replacement` _string_ | | | |


#### QueryTrackerSpec
Expand Down Expand Up @@ -1336,6 +1354,7 @@ _Appears in:_

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `observedGeneration` _integer_ | Reflects resource generation which was used for updating status. | | |
| `releaseStatus` _[RemoteNodeReleaseStatus](#remotenodereleasestatus)_ | | | |


Expand Down Expand Up @@ -1435,6 +1454,7 @@ _Appears in:_

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `observedGeneration` _integer_ | Reflects resource generation which was used for updating status. | | |
| `releaseStatus` _[RemoteNodeReleaseStatus](#remotenodereleasestatus)_ | | | |


Expand Down Expand Up @@ -1567,6 +1587,7 @@ _Appears in:_

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `observedGeneration` _integer_ | Reflects resource generation which was used for updating status. | | |
| `releaseStatus` _[RemoteNodeReleaseStatus](#remotenodereleasestatus)_ | | | |


Expand Down Expand Up @@ -1777,7 +1798,7 @@ _Appears in:_
| `nodeSelector` _object (keys:string, values:string)_ | | | |
| `externalProxy` _string_ | | | |
| `controllerFamilies` _string array_ | Supported controller families, for example: "chyt", "jupyt", "livy". | | |
| `defaultRouteFamily` _string_ | The family that will receive requests for domains that are not explicitly specified in http_controller_mappings.<br />For example, "chyt" (with `ControllerFamilies` set to {"chyt", "jupyt"} would mean<br />that requests to "foo.<domain>" will be processed by chyt controller. | | |
| `defaultRouteFamily` _string_ | The family that will receive requests for domains that are not explicitly specified in http_controller_mappings.<br />For example, "chyt" (with `ControllerFamilies` set to \{"chyt", "jupyt"\} would mean<br />that requests to "foo.<domain>" will be processed by chyt controller. | | |


#### StructuredLoggerSpec
Expand Down
6 changes: 6 additions & 0 deletions pkg/ytconfig/canondata/TestGetHTTPProxyConfig/test.canondata
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
secure=%true;
"user_info_endpoint"="user-info-endpoint";
"user_info_login_field"=login;
"login_transformations"=[
{
"match_pattern"="(.*)@ytsaurus.team";
replacement="\\1";
};
];
};
"oauth_cookie_authenticator"={
};
Expand Down
6 changes: 6 additions & 0 deletions pkg/ytconfig/canondata/TestGetRPCProxyConfig/test.canondata
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
secure=%true;
"user_info_endpoint"="user-info-endpoint";
"user_info_login_field"=login;
"login_transformations"=[
{
"match_pattern"="(.*)@ytsaurus.team";
replacement="\\1";
};
];
};
"oauth_token_authenticator"={
};
Expand Down
38 changes: 22 additions & 16 deletions pkg/ytconfig/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,22 +436,35 @@ func (g *Generator) getRPCProxyConfigImpl(spec *ytv1.RPCProxiesSpec) (RPCProxySe
g.fillCommonService(&c.CommonServer, &spec.InstanceSpec)

if g.ytsaurus.Spec.OauthService != nil {
c.OauthService = ptr.To(getOauthService(*g.ytsaurus.Spec.OauthService))
c.CypressUserManager = CypressUserManager{}
c.OauthService = &OauthService{
Host: g.ytsaurus.Spec.OauthService.Host,
Port: g.ytsaurus.Spec.OauthService.Port,
Secure: g.ytsaurus.Spec.OauthService.Secure,
UserInfoEndpoint: g.ytsaurus.Spec.OauthService.UserInfo.Endpoint,
UserInfoLoginField: g.ytsaurus.Spec.OauthService.UserInfo.LoginField,
UserInfoErrorField: g.ytsaurus.Spec.OauthService.UserInfo.ErrorField,
}
c.OauthTokenAuthenticator = &OauthTokenAuthenticator{}
c.RequireAuthentication = ptr.To(true)
}

return c, nil
}

func getOauthService(oauthServiceSpec ytv1.OauthServiceSpec) OauthService {
var loginTransformations []LoginTransformation
for _, tr := range oauthServiceSpec.UserInfo.LoginTransformations {
loginTransformations = append(loginTransformations, LoginTransformation{
MatchPattern: tr.MatchPattern,
Replacement: tr.Replacement,
})
}

return OauthService{
Host: oauthServiceSpec.Host,
Port: oauthServiceSpec.Port,
Secure: oauthServiceSpec.Secure,
UserInfoEndpoint: oauthServiceSpec.UserInfo.Endpoint,
UserInfoLoginField: oauthServiceSpec.UserInfo.LoginField,
UserInfoErrorField: oauthServiceSpec.UserInfo.ErrorField,
LoginTransformations: loginTransformations,
}
}

func (g *Generator) GetRPCProxyConfig(spec ytv1.RPCProxiesSpec) ([]byte, error) {
c, err := g.getRPCProxyConfigImpl(&spec)
if err != nil {
Expand Down Expand Up @@ -598,14 +611,7 @@ func (g *Generator) getHTTPProxyConfigImpl(spec *ytv1.HTTPProxiesSpec) (HTTPProx
g.fillBusServer(&c.CommonServer, spec.NativeTransport)

if g.ytsaurus.Spec.OauthService != nil {
c.Auth.OauthService = &OauthService{
Host: g.ytsaurus.Spec.OauthService.Host,
Port: g.ytsaurus.Spec.OauthService.Port,
Secure: g.ytsaurus.Spec.OauthService.Secure,
UserInfoEndpoint: g.ytsaurus.Spec.OauthService.UserInfo.Endpoint,
UserInfoLoginField: g.ytsaurus.Spec.OauthService.UserInfo.LoginField,
UserInfoErrorField: g.ytsaurus.Spec.OauthService.UserInfo.ErrorField,
}
c.Auth.OauthService = ptr.To(getOauthService(*g.ytsaurus.Spec.OauthService))
c.Auth.OauthCookieAuthenticator = &OauthCookieAuthenticator{}
c.Auth.OauthTokenAuthenticator = &OauthTokenAuthenticator{}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/ytconfig/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,12 @@ func withOauthSpec(ytsaurus *ytv1.Ytsaurus) *ytv1.Ytsaurus {
UserInfo: ytv1.OauthUserInfoHandlerSpec{
Endpoint: "user-info-endpoint",
LoginField: "login",
LoginTransformations: []ytv1.OauthUserLoginTransformation{
{
MatchPattern: "(.*)@ytsaurus.team",
Replacement: `\1`,
},
},
},
}
return ytsaurus
Expand Down
18 changes: 12 additions & 6 deletions pkg/ytconfig/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ type CypressTokenAuthenticator struct {
}

type OauthService struct {
Host string `yson:"host"`
Port int `yson:"port"`
Secure bool `yson:"secure"`
UserInfoEndpoint string `yson:"user_info_endpoint"`
UserInfoLoginField string `yson:"user_info_login_field"`
UserInfoErrorField *string `yson:"user_info_error_field,omitempty"`
Host string `yson:"host"`
Port int `yson:"port"`
Secure bool `yson:"secure"`
UserInfoEndpoint string `yson:"user_info_endpoint"`
UserInfoLoginField string `yson:"user_info_login_field"`
UserInfoErrorField *string `yson:"user_info_error_field,omitempty"`
LoginTransformations []LoginTransformation `yson:"login_transformations,omitempty"`
}

type LoginTransformation struct {
MatchPattern string `yson:"match_pattern,omitempty"`
Replacement string `yson:"replacement,omitempty"`
}

type OauthCookieAuthenticator struct{}
Expand Down
13 changes: 13 additions & 0 deletions ytop-chart/templates/crds/ytsaurus.cluster.ytsaurus.tech.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14923,6 +14923,19 @@ spec:
loginField:
default: nickname
type: string
loginTransformations:
description: LoginTransformations will be applied to the login
field consequentially if set.
items:
properties:
matchPattern:
description: MatchPattern expects RE2 (https://github.com/google/re2/wiki/syntax)
syntax.
type: string
replacement:
type: string
type: object
type: array
type: object
type: object
primaryMasters:
Expand Down
Loading