Skip to content

Commit 7477257

Browse files
authored
✨ Broker KAI api through hub. (#750)
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]>
1 parent 58960d8 commit 7477257

File tree

7 files changed

+143
-9
lines changed

7 files changed

+143
-9
lines changed

.github/workflows/main.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v4
1616
- uses: actions/setup-go@v3
1717
with:
18-
go-version: '1.19'
18+
go-version: '1.21'
1919
- run: make fmt
2020

2121
vet:
@@ -24,7 +24,7 @@ jobs:
2424
- uses: actions/checkout@v4
2525
- uses: actions/setup-go@v3
2626
with:
27-
go-version: '1.19'
27+
go-version: '1.21'
2828
- run: make vet
2929

3030
build:
@@ -33,7 +33,7 @@ jobs:
3333
- uses: actions/checkout@v4
3434
- uses: actions/setup-go@v3
3535
with:
36-
go-version: '1.19'
36+
go-version: '1.21'
3737
- run: make cmd
3838

3939
test-unit:
@@ -42,7 +42,7 @@ jobs:
4242
- uses: actions/checkout@v4
4343
- uses: actions/setup-go@v3
4444
with:
45-
go-version: '1.19'
45+
go-version: '1.21'
4646
- run: make test
4747

4848
test-api:
@@ -60,7 +60,7 @@ jobs:
6060
- uses: actions/checkout@v4
6161
- uses: actions/setup-go@v3
6262
with:
63-
go-version: '1.19'
63+
go-version: '1.21'
6464
- run: |
6565
rm -f $DB_PATH
6666
make run &

.github/workflows/test-nightly.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v3
1212
- uses: actions/setup-go@v3
1313
with:
14-
go-version: '1.19'
14+
go-version: '1.21'
1515
- run: make test
1616

1717
test-api:
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/checkout@v3
2121
- uses: actions/setup-go@v3
2222
with:
23-
go-version: '1.19'
23+
go-version: '1.21'
2424
- run: |
2525
make vet
2626
DISCONNECTED=1 make run &

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ $(CONTROLLERGEN):
8484

8585
# Ensure goimports installed.
8686
$(GOIMPORTS):
87-
go install golang.org/x/tools/cmd/goimports@latest
87+
go install golang.org/x/tools/cmd/goimports@v0.24
8888

8989
# Build SAMPLE ADDON
9090
addon: fmt vet

api/error.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"errors"
5+
"fmt"
56
"net/http"
67
"os"
78

@@ -78,6 +79,22 @@ func (r *Forbidden) Is(err error) (matched bool) {
7879
return
7980
}
8081

82+
// NotFound reports resource not-found errors.
83+
type NotFound struct {
84+
Resource string
85+
Reason string
86+
}
87+
88+
func (r *NotFound) Error() string {
89+
return fmt.Sprintf("Resource '%s' not found. %s", r.Resource, r.Reason)
90+
}
91+
92+
func (r *NotFound) Is(err error) (matched bool) {
93+
var forbidden *Forbidden
94+
matched = errors.As(err, &forbidden)
95+
return
96+
}
97+
8198
// ErrorHandler handles error conditions from lower handlers.
8299
func ErrorHandler() gin.HandlerFunc {
83100
return func(ctx *gin.Context) {
@@ -102,7 +119,8 @@ func ErrorHandler() gin.HandlerFunc {
102119
return
103120
}
104121

105-
if errors.Is(err, gorm.ErrRecordNotFound) {
122+
if errors.Is(err, gorm.ErrRecordNotFound) ||
123+
errors.Is(err, &NotFound{}) {
106124
if ctx.Request.Method == http.MethodDelete {
107125
rtx.Status(http.StatusNoContent)
108126
return

api/pkg.go

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func All() []Handler {
7878
&RuleSetHandler{},
7979
&SchemaHandler{},
8080
&SettingHandler{},
81+
&ServiceHandler{},
8182
&StakeholderHandler{},
8283
&StakeholderGroupHandler{},
8384
&TagHandler{},

api/service.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httputil"
7+
"net/url"
8+
"os"
9+
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
// Routes
14+
const (
15+
ServicesRoot = "/services"
16+
ServiceRoot = ServicesRoot + "/:name/*" + Wildcard
17+
)
18+
19+
// serviceRoutes name to route map.
20+
var serviceRoutes = map[string]string{
21+
"kai": os.Getenv("KAI_URL"),
22+
}
23+
24+
// ServiceHandler handles service routes.
25+
type ServiceHandler struct {
26+
BaseHandler
27+
}
28+
29+
// AddRoutes adds routes.
30+
func (h ServiceHandler) AddRoutes(e *gin.Engine) {
31+
e.GET(ServicesRoot, h.List)
32+
e.Any(ServiceRoot, h.Required, h.Forward)
33+
}
34+
35+
// List godoc
36+
// @summary List named service routes.
37+
// @description List named service routes.
38+
// @tags services
39+
// @produce json
40+
// @success 200 {object} api.Service
41+
// @router /services [get]
42+
func (h ServiceHandler) List(ctx *gin.Context) {
43+
var r []Service
44+
for name, route := range serviceRoutes {
45+
service := Service{Name: name, Route: route}
46+
r = append(r, service)
47+
}
48+
49+
h.Respond(ctx, http.StatusOK, r)
50+
}
51+
52+
// Required enforces RBAC.
53+
func (h ServiceHandler) Required(ctx *gin.Context) {
54+
Required(ctx.Param(Name))(ctx)
55+
}
56+
57+
// Forward provides RBAC and forwards request to the service.
58+
func (h ServiceHandler) Forward(ctx *gin.Context) {
59+
path := ctx.Param(Wildcard)
60+
name := ctx.Param(Name)
61+
route, found := serviceRoutes[name]
62+
if !found {
63+
err := &NotFound{Resource: name}
64+
_ = ctx.Error(err)
65+
return
66+
}
67+
if route == "" {
68+
err := fmt.Errorf("route for: '%s' not defined", name)
69+
_ = ctx.Error(err)
70+
return
71+
}
72+
u, err := url.Parse(route)
73+
if err != nil {
74+
err = &BadRequestError{Reason: err.Error()}
75+
_ = ctx.Error(err)
76+
return
77+
}
78+
proxy := httputil.ReverseProxy{
79+
Director: func(req *http.Request) {
80+
req.URL.Scheme = u.Scheme
81+
req.URL.Host = u.Host
82+
req.URL.Path = path
83+
Log.Info(
84+
"Routing (service)",
85+
"path",
86+
ctx.Request.URL.Path,
87+
"route",
88+
req.URL.String())
89+
},
90+
}
91+
92+
proxy.ServeHTTP(ctx.Writer, ctx.Request)
93+
}
94+
95+
// Service REST resource.
96+
type Service struct {
97+
Name string `json:"name"`
98+
Route string `json:"route"`
99+
}

auth/roles.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
- get
8383
- post
8484
- put
85+
- name: kai
86+
verbs:
87+
- get
88+
- post
8589
- name: proxies
8690
verbs:
8791
- delete
@@ -286,6 +290,10 @@
286290
- get
287291
- post
288292
- put
293+
- name: kai
294+
verbs:
295+
- get
296+
- post
289297
- name: proxies
290298
verbs:
291299
- get
@@ -443,6 +451,10 @@
443451
- name: jobfunctions
444452
verbs:
445453
- get
454+
- name: kai
455+
verbs:
456+
- get
457+
- post
446458
- name: proxies
447459
verbs:
448460
- get
@@ -560,6 +572,10 @@
560572
- name: jobfunctions
561573
verbs:
562574
- get
575+
- name: kai
576+
verbs:
577+
- get
578+
- post
563579
- name: proxies
564580
verbs:
565581
- get

0 commit comments

Comments
 (0)