From c88c97d226dfcf755fd470fb3dd001238ca0e825 Mon Sep 17 00:00:00 2001 From: stonezdj Date: Tue, 14 Nov 2023 11:09:36 +0800 Subject: [PATCH] Add oidc provider name to systeminfo API fixes #13198 Signed-off-by: stonezdj --- api/v2.0/swagger.yaml | 5 +++++ src/controller/systeminfo/controller.go | 10 +++++++++ src/controller/systeminfo/controller_test.go | 22 ++++++++++++++++++++ src/server/v2.0/handler/systeminfo.go | 1 + 4 files changed, 38 insertions(+) diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index ba4dcd770db..37e8564eb67 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -7867,6 +7867,11 @@ definitions: x-nullable: true x-omitempty: true $ref: '#/definitions/AuthproxySetting' + oidc_provider_name: + type: string + x-nullable: true + x-omitempty: true + description: The OIDC provider name, empty if current auth is not OIDC_auth or OIDC provider is not configured. AuthproxySetting: type: object properties: diff --git a/src/controller/systeminfo/controller.go b/src/controller/systeminfo/controller.go index 1487f036bb7..3e1aa93e70f 100644 --- a/src/controller/systeminfo/controller.go +++ b/src/controller/systeminfo/controller.go @@ -50,6 +50,7 @@ type Data struct { BannerMessage string AuthProxySettings *models.HTTPAuthProxy Protected *protectedData + OIDCProviderName string } type protectedData struct { @@ -103,6 +104,7 @@ func (c *controller) GetInfo(ctx context.Context, opt Options) (*Data, error) { SelfRegistration: utils.SafeCastBool(cfg[common.SelfRegistration]), HarborVersion: fmt.Sprintf("%s-%s", version.ReleaseVersion, version.GitCommit), BannerMessage: utils.SafeCastString(mgr.Get(ctx, common.BannerMessage).GetString()), + OIDCProviderName: OIDCProviderName(cfg), } if res.AuthMode == common.HTTPAuth { if s, err := config.HTTPAuthProxySetting(ctx); err == nil { @@ -137,6 +139,14 @@ func (c *controller) GetInfo(ctx context.Context, opt Options) (*Data, error) { return res, nil } +func OIDCProviderName(cfg map[string]interface{}) string { + authMode := utils.SafeCastString(cfg[common.AUTHMode]) + if authMode != common.OIDCAuth { + return "" + } + return utils.SafeCastString(cfg[common.OIDCName]) +} + func (c *controller) GetCapacity(ctx context.Context) (*imagestorage.Capacity, error) { systeminfo.Init() return imagestorage.GlobalDriver.Cap() diff --git a/src/controller/systeminfo/controller_test.go b/src/controller/systeminfo/controller_test.go index a2e98a1ba74..864d43a99d6 100644 --- a/src/controller/systeminfo/controller_test.go +++ b/src/controller/systeminfo/controller_test.go @@ -105,3 +105,25 @@ func (s *sysInfoCtlTestSuite) TestGetInfo() { func TestControllerSuite(t *testing.T) { suite.Run(t, &sysInfoCtlTestSuite{}) } + +func TestOIDCProviderName(t *testing.T) { + type args struct { + cfg map[string]interface{} + } + tests := []struct { + name string + args args + want string + }{ + {"normal testing", args{map[string]interface{}{common.AUTHMode: common.OIDCAuth, common.OIDCName: "test"}}, "test"}, + {"not oidc", args{map[string]interface{}{common.AUTHMode: common.DBAuth, common.OIDCName: "test"}}, ""}, + {"empty provider", args{map[string]interface{}{common.AUTHMode: common.OIDCAuth, common.OIDCName: ""}}, ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := OIDCProviderName(tt.args.cfg); got != tt.want { + t.Errorf("OIDCProviderName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/src/server/v2.0/handler/systeminfo.go b/src/server/v2.0/handler/systeminfo.go index c2d3435ac26..5d7e46f9529 100644 --- a/src/server/v2.0/handler/systeminfo.go +++ b/src/server/v2.0/handler/systeminfo.go @@ -87,6 +87,7 @@ func (s *sysInfoAPI) convertInfo(d *si.Data) *models.GeneralInfo { SelfRegistration: &d.SelfRegistration, HarborVersion: &d.HarborVersion, BannerMessage: &d.BannerMessage, + OIDCProviderName: &d.OIDCProviderName, } if d.AuthProxySettings != nil { res.AuthproxySettings = &models.AuthproxySetting{