Skip to content

Commit

Permalink
Switch API to ping OIDC endpoint to new model
Browse files Browse the repository at this point in the history
This commit updates the API POST /api/v2.0/system/oidc/ping to new
programming model, in which the code will be generated by go-swagger.

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
  • Loading branch information
reasonerjt committed Mar 4, 2021
1 parent d36994b commit e96c1cb
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 166 deletions.
31 changes: 0 additions & 31 deletions api/v2.0/legacy_swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1737,37 +1737,6 @@ paths:
$ref: '#/definitions/NotFoundChartAPIError'
'500':
$ref: '#/definitions/InternalChartAPIError'
'/system/oidc/ping':
post:
summary: Test the OIDC endpoint.
description: Test the OIDC endpoint, the setting of the endpoint is provided in the request. This API can only
be called by system admin.
tags:
- Products
- System
parameters:
- name: endpoint
in: body
description: Request body for OIDC endpoint to be tested.
required: true
schema:
type: object
properties:
url:
type: string
description: The URL of OIDC endpoint to be tested.
verify_cert:
type: boolean
description: Whether the certificate should be verified
responses:
'200':
description: Ping succeeded. The OIDC endpoint is valid.
'400':
description: The ping failed
'401':
description: User need to log in first.
'403':
description: User does not have permission to call this API
'/system/CVEAllowlist':
get:
summary: Get the system level allowlist of CVE.
Expand Down
31 changes: 31 additions & 0 deletions api/v2.0/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,37 @@ paths:
description: Not found the default root certificate.
'500':
$ref: '#/responses/500'
/system/oidc/ping:
post:
summary: Test the OIDC endpoint.
description: |
Test the OIDC endpoint, the setting of the endpoint is provided in the request. This API can only be called by system admin.
tags:
- oidc
operationId: pingOIDC
parameters:
- name: endpoint
in: body
description: Request body for OIDC endpoint to be tested.
required: true
schema:
type: object
properties:
url:
type: string
description: The URL of OIDC endpoint to be tested.
verify_cert:
type: boolean
description: Whether the certificate should be verified
responses:
'200':
$ref: '#/responses/200'
'400':
$ref: '#/responses/400'
'401':
$ref: '#/responses/401'
'403':
$ref: '#/responses/403'
/system/gc:
get:
summary: Get gc results.
Expand Down
1 change: 1 addition & 0 deletions src/common/rbac/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ const (
ResourceReplicationPolicy = Resource("replication-policy")
ResourceScanAll = Resource("scan-all")
ResourceSystemVolumes = Resource("system-volumes")
ResourceOIDCEndpoint = Resource("oidc-endpoint")
)
3 changes: 3 additions & 0 deletions src/common/rbac/system/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ var (
{Resource: rbac.ResourceScanAll, Action: rbac.ActionList},

{Resource: rbac.ResourceSystemVolumes, Action: rbac.ActionRead},

{Resource: rbac.ResourceOIDCEndpoint, Action: rbac.ActionUpdate},
{Resource: rbac.ResourceOIDCEndpoint, Action: rbac.ActionRead},
}
)
1 change: 0 additions & 1 deletion src/core/api/harborapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func init() {
beego.Router("/api/labels", &LabelAPI{}, "post:Post;get:List")
beego.Router("/api/labels/:id([0-9]+", &LabelAPI{}, "get:Get;put:Put;delete:Delete")
beego.Router("/api/system/CVEAllowlist", &SysCVEAllowlistAPI{}, "get:Get;put:Put")
beego.Router("/api/system/oidc/ping", &OIDCAPI{}, "post:Ping")

beego.Router("/api/replication/adapters", &ReplicationAdapterAPI{}, "get:List")

Expand Down
57 changes: 0 additions & 57 deletions src/core/api/oidc.go

This file was deleted.

69 changes: 0 additions & 69 deletions src/core/api/oidc_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/controllers/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/common/utils/oidc"
"github.com/goharbor/harbor/src/core/api"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/oidc"
)

const tokenKey = "oidc_token"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package oidc
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSecretVerifyError(t *testing.T) {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/server/middleware/security/idtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/security/local"
"github.com/goharbor/harbor/src/common/utils/oidc"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/oidc"
)

type idToken struct{}
Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/security/oidc_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/goharbor/harbor/src/common/api"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/security/local"
"github.com/goharbor/harbor/src/common/utils/oidc"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/oidc"
)

var (
Expand Down
7 changes: 4 additions & 3 deletions src/server/middleware/security/oidc_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package security

import (
"fmt"
"net/http"
"testing"

"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/utils/oidc"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/pkg/oidc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

func TestOIDCCli(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions src/server/v2.0/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func New() http.Handler {
GCAPI: newGCAPI(),
QuotaAPI: newQuotaAPI(),
RetentionAPI: newRetentionAPI(),
OidcAPI: newOIDCAPI(),
})
if err != nil {
log.Fatal(err)
Expand Down
36 changes: 36 additions & 0 deletions src/server/v2.0/handler/oidc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handler

import (
"context"

"github.com/go-openapi/runtime/middleware"
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
oidcpkg "github.com/goharbor/harbor/src/pkg/oidc"
"github.com/goharbor/harbor/src/server/v2.0/restapi/operations/oidc"
)

type oidcAPI struct {
BaseAPI
}

func newOIDCAPI() *oidcAPI {
return &oidcAPI{}
}

func (o oidcAPI) PingOIDC(ctx context.Context, params oidc.PingOIDCParams) middleware.Responder {
if err := o.RequireSystemAccess(ctx, rbac.ActionUpdate, rbac.ResourceOIDCEndpoint); err != nil {
return o.SendError(ctx, err)
}
err := oidcpkg.TestEndpoint(oidcpkg.Conn{
URL: params.Endpoint.URL,
VerifyCert: params.Endpoint.VerifyCert,
})

if err != nil {
log.Errorf("Failed to verify connection: %+v, err: %v", params.Endpoint, err)
return o.SendError(ctx, errors.New(nil).WithCode(errors.BadRequestCode).WithMessage("failed to verify connection"))
}
return oidc.NewPingOIDCOK()
}
1 change: 0 additions & 1 deletion src/server/v2.0/route/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func registerLegacyRoutes() {
beego.Router("/api/"+version+"/projects/:id([0-9]+)/metadatas/", &api.MetadataAPI{}, "post:Post")

beego.Router("/api/"+version+"/system/CVEAllowlist", &api.SysCVEAllowlistAPI{}, "get:Get;put:Put")
beego.Router("/api/"+version+"/system/oidc/ping", &api.OIDCAPI{}, "post:Ping")

beego.Router("/api/"+version+"/replication/adapters", &api.ReplicationAdapterAPI{}, "get:List")
beego.Router("/api/"+version+"/replication/adapterinfos", &api.ReplicationAdapterAPI{}, "get:ListAdapterInfos")
Expand Down

0 comments on commit e96c1cb

Please sign in to comment.