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

✨ Broker KAI api through hub. #750

Merged
merged 11 commits into from
Sep 13, 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
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
Loading