Skip to content

Commit 6f3fa77

Browse files
committed
lgos
1 parent 0f1b8ac commit 6f3fa77

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

pkg/proxy/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (s Server) AuthMiddleware(next http.Handler) http.Handler {
160160
requestPath := strings.Trim(r.URL.Path, "/")
161161
requestAPIPath := ""
162162
if len(requestPath) >= len(apiPath) && requestPath[:len(apiPath)] == apiPath {
163-
requestAPIPath = requestPath[len(apiPath):]
163+
requestAPIPath = requestPath[len(apiPath)+1:]
164164
}
165165

166166
// Handle white listed paths

pkg/proxy/token.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package proxy
33
import (
44
"crypto/rsa"
55
"fmt"
6-
"log"
76
"strings"
87

98
"github.com/dgrijalva/jwt-go"
@@ -26,34 +25,53 @@ func authenticateToken(token string, secret []byte, publicKey *rsa.PublicKey) (*
2625
return tok, err
2726
}
2827

29-
func getTokenData(claims jwt.MapClaims) ocgatev1beta1.GateToken {
30-
var t ocgatev1beta1.GateToken
31-
var ok bool
32-
33-
log.Printf("t.Status.Data [%+v]", t.Status.Data)
28+
func getTokenData(claims jwt.MapClaims) *ocgatev1beta1.GateToken {
29+
t := &ocgatev1beta1.GateToken{
30+
Status: ocgatev1beta1.GateTokenStatus{
31+
Data: ocgatev1beta1.GateTokenCache{
32+
Namespace: "*",
33+
Verbs: []string{"get"},
34+
},
35+
},
36+
}
3437

35-
if t.Status.Data.Namespace, ok = claims["namespace"].(string); !ok {
36-
t.Status.Data.Namespace = "*"
38+
if namespace, ok := claims["namespace"].(string); ok {
39+
t.Status.Data.Namespace = namespace
3740
}
3841

39-
if t.Status.Data.Verbs, ok = claims["verbs"].([]string); !ok {
40-
t.Status.Data.Verbs = []string{"get"}
42+
if verbs, ok := claims["verbs"].([]interface{}); ok {
43+
t.Status.Data.Verbs = make([]string, len(verbs))
44+
for i, v := range verbs {
45+
t.Status.Data.Verbs[i] = v.(string)
46+
}
4147
}
4248

43-
if t.Status.Data.APIGroups, ok = claims["apiGroups"].([]string); !ok {
44-
t.Status.Data.APIGroups = nil
49+
if apiGroups, ok := claims["apiGroups"].([]interface{}); ok {
50+
t.Status.Data.APIGroups = make([]string, len(apiGroups))
51+
for i, v := range apiGroups {
52+
t.Status.Data.APIGroups[i] = v.(string)
53+
}
4554
}
4655

47-
if t.Status.Data.Resources, ok = claims["resources"].([]string); !ok {
48-
t.Status.Data.Resources = nil
56+
if resources, ok := claims["resources"].([]interface{}); ok {
57+
t.Status.Data.Resources = make([]string, len(resources))
58+
for i, v := range resources {
59+
t.Status.Data.Resources[i] = v.(string)
60+
}
4961
}
5062

51-
if t.Status.Data.ResourceNames, ok = claims["resourceNames"].([]string); !ok {
52-
t.Status.Data.ResourceNames = nil
63+
if resourceNames, ok := claims["resourceNames"].([]interface{}); ok {
64+
t.Status.Data.ResourceNames = make([]string, len(resourceNames))
65+
for i, v := range resourceNames {
66+
t.Status.Data.ResourceNames[i] = v.(string)
67+
}
5368
}
5469

55-
if t.Status.Data.NonResourceURLs, ok = claims["nonResourceURLs"].([]string); !ok {
56-
t.Status.Data.NonResourceURLs = nil
70+
if nonResourceURLs, ok := claims["nonResourceURLs"].([]interface{}); ok {
71+
t.Status.Data.NonResourceURLs = make([]string, len(nonResourceURLs))
72+
for i, v := range nonResourceURLs {
73+
t.Status.Data.NonResourceURLs[i] = v.(string)
74+
}
5775
}
5876

5977
return t
@@ -73,11 +91,11 @@ func getRequstResource(request string) (namespace string, apiGroup string, resou
7391
apiGroup = ""
7492
requestList = requestList[2:]
7593
} else {
76-
apiGroup = requestList[2]
94+
apiGroup = requestList[1]
7795
requestList = requestList[3:]
7896
}
7997

80-
if len(requestList) >= 2 && requestList[0] == "namespace" {
98+
if len(requestList) >= 2 && len(requestList[0]) >= 9 && requestList[0][:9] == "namespace" {
8199
namespace = requestList[1]
82100
requestList = requestList[2:]
83101
}
@@ -160,9 +178,6 @@ func authorizeTokenClamis(claims jwt.MapClaims, requestMethod string, requestAPI
160178
verb, _ := getRequestVerb(requestMethod)
161179
namespace, apiGroup, resource, resourceName := getRequstResource(requestAPIPath)
162180

163-
log.Printf("claims: [%+v]", t.Status.Data)
164-
log.Printf("request: %s [%s, %s, %s, %s]", verb, namespace, apiGroup, resource, resourceName)
165-
166181
// Verifiy verb
167182
if t.Status.Data.Verbs == nil || !contains(t.Status.Data.Verbs, verb) {
168183
return fmt.Errorf("verb (%s) is not permited", verb)

0 commit comments

Comments
 (0)