Skip to content

Commit

Permalink
✨ Broker KAI api through hub. (#750)
Browse files Browse the repository at this point in the history
Add `/services/` endpoint.
Add `/services/kai/*` endpoint reverse-proxy to route defined in
KAI_URL.
Add auth scopes.

Related: konveyor/operator#376

---------

Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel authored and dymurray committed Oct 11, 2024
1 parent 384d7f8 commit 7fe3d6a
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 9 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: make fmt

vet:
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: make vet

build:
Expand All @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: make cmd

test-unit:
Expand All @@ -42,7 +42,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: make test

test-api:
Expand All @@ -60,7 +60,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: |
rm -f $DB_PATH
make run &
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: make test

test-api:
Expand All @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- run: |
make vet
DISCONNECTED=1 make run &
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $(CONTROLLERGEN):

# Ensure goimports installed.
$(GOIMPORTS):
go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/tools/cmd/goimports@v0.24

# Build SAMPLE ADDON
addon: fmt vet
Expand Down
20 changes: 19 additions & 1 deletion api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"errors"
"fmt"
"net/http"
"os"

Expand Down Expand Up @@ -78,6 +79,22 @@ func (r *Forbidden) Is(err error) (matched bool) {
return
}

// NotFound reports resource not-found errors.
type NotFound struct {
Resource string
Reason string
}

func (r *NotFound) Error() string {
return fmt.Sprintf("Resource '%s' not found. %s", r.Resource, r.Reason)
}

func (r *NotFound) Is(err error) (matched bool) {
var forbidden *Forbidden
matched = errors.As(err, &forbidden)
return
}

// ErrorHandler handles error conditions from lower handlers.
func ErrorHandler() gin.HandlerFunc {
return func(ctx *gin.Context) {
Expand All @@ -102,7 +119,8 @@ func ErrorHandler() gin.HandlerFunc {
return
}

if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, gorm.ErrRecordNotFound) ||
errors.Is(err, &NotFound{}) {
if ctx.Request.Method == http.MethodDelete {
rtx.Status(http.StatusNoContent)
return
Expand Down
1 change: 1 addition & 0 deletions api/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func All() []Handler {
&RuleSetHandler{},
&SchemaHandler{},
&SettingHandler{},
&ServiceHandler{},
&StakeholderHandler{},
&StakeholderGroupHandler{},
&TagHandler{},
Expand Down
99 changes: 99 additions & 0 deletions api/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package api

import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"

"github.com/gin-gonic/gin"
)

// Routes
const (
ServicesRoot = "/services"
ServiceRoot = ServicesRoot + "/:name/*" + Wildcard
)

// serviceRoutes name to route map.
var serviceRoutes = map[string]string{
"kai": os.Getenv("KAI_URL"),
}

// ServiceHandler handles service routes.
type ServiceHandler struct {
BaseHandler
}

// AddRoutes adds routes.
func (h ServiceHandler) AddRoutes(e *gin.Engine) {
e.GET(ServicesRoot, h.List)
e.Any(ServiceRoot, h.Required, h.Forward)
}

// List godoc
// @summary List named service routes.
// @description List named service routes.
// @tags services
// @produce json
// @success 200 {object} api.Service
// @router /services [get]
func (h ServiceHandler) List(ctx *gin.Context) {
var r []Service
for name, route := range serviceRoutes {
service := Service{Name: name, Route: route}
r = append(r, service)
}

h.Respond(ctx, http.StatusOK, r)
}

// Required enforces RBAC.
func (h ServiceHandler) Required(ctx *gin.Context) {
Required(ctx.Param(Name))(ctx)
}

// Forward provides RBAC and forwards request to the service.
func (h ServiceHandler) Forward(ctx *gin.Context) {
path := ctx.Param(Wildcard)
name := ctx.Param(Name)
route, found := serviceRoutes[name]
if !found {
err := &NotFound{Resource: name}
_ = ctx.Error(err)
return
}
if route == "" {
err := fmt.Errorf("route for: '%s' not defined", name)
_ = ctx.Error(err)
return
}
u, err := url.Parse(route)
if err != nil {
err = &BadRequestError{Reason: err.Error()}
_ = ctx.Error(err)
return
}
proxy := httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = u.Scheme
req.URL.Host = u.Host
req.URL.Path = path
Log.Info(
"Routing (service)",
"path",
ctx.Request.URL.Path,
"route",
req.URL.String())
},
}

proxy.ServeHTTP(ctx.Writer, ctx.Request)
}

// Service REST resource.
type Service struct {
Name string `json:"name"`
Route string `json:"route"`
}
16 changes: 16 additions & 0 deletions auth/roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
- get
- post
- put
- name: kai
verbs:
- get
- post
- name: proxies
verbs:
- delete
Expand Down Expand Up @@ -286,6 +290,10 @@
- get
- post
- put
- name: kai
verbs:
- get
- post
- name: proxies
verbs:
- get
Expand Down Expand Up @@ -443,6 +451,10 @@
- name: jobfunctions
verbs:
- get
- name: kai
verbs:
- get
- post
- name: proxies
verbs:
- get
Expand Down Expand Up @@ -560,6 +572,10 @@
- name: jobfunctions
verbs:
- get
- name: kai
verbs:
- get
- post
- name: proxies
verbs:
- get
Expand Down

0 comments on commit 7fe3d6a

Please sign in to comment.