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

Gate #57

Merged
merged 32 commits into from
Oct 3, 2022
Merged

Gate #57

Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ff2abea
guard-gate
davidhadas Sep 25, 2022
b0f25d1
set ns guard-service as default
davidhadas Sep 25, 2022
b80c3f1
test correction
davidhadas Sep 25, 2022
ceb4fa1
test correction
davidhadas Sep 25, 2022
47634cc
fix time based tests
davidhadas Sep 26, 2022
a9e412f
fix time based tests
davidhadas Sep 26, 2022
f77de08
moved to go cancel()
davidhadas Sep 26, 2022
ebe577e
moved MinimalInterval to const to avoid race during tests
davidhadas Sep 26, 2022
909b5d9
moved MinimalInterval to const to avoid race during tests
davidhadas Sep 26, 2022
501ed39
avoiding race in ApproveResponse
davidhadas Sep 26, 2022
5034900
fixed fmt.printf use
davidhadas Sep 26, 2022
17d57a2
alignment
davidhadas Sep 26, 2022
b815b08
updates
davidhadas Sep 26, 2022
f154ee6
Merge branch 'knative-sandbox:main' into gate
davidhadas Sep 26, 2022
6aa2d69
Merge branch 'knative-sandbox:main' into gate
davidhadas Sep 27, 2022
f13a27a
bug fix http.client
davidhadas Sep 29, 2022
ad6ed73
nits
davidhadas Sep 29, 2022
bdfbf79
nits
davidhadas Sep 29, 2022
699af57
deployment of services
davidhadas Sep 29, 2022
74f74ae
mv deploy config + add cmd/queue/main.go
davidhadas Sep 29, 2022
5240423
mv deploy config + add cmd/queue/main.go
davidhadas Sep 29, 2022
aa6f2f1
mv deploy config + add cmd/queue/main.go
davidhadas Sep 29, 2022
6abdac6
fix bugs
davidhadas Sep 30, 2022
78e78b7
fix bugs
davidhadas Sep 30, 2022
2f29942
fix bugs
davidhadas Sep 30, 2022
ca2ea32
fix bugs
davidhadas Sep 30, 2022
722b424
fix bugs
davidhadas Sep 30, 2022
21a2803
fix bugs
davidhadas Sep 30, 2022
4e7abe9
fix bugs
davidhadas Sep 30, 2022
bf92ec5
review comments
davidhadas Oct 1, 2022
68080b9
review comments
davidhadas Oct 1, 2022
098db68
Update config/deploy/guard-service.yaml
davidhadas Oct 3, 2022
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
1 change: 1 addition & 0 deletions cmd/guard-service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ package main

// Uncomment when running in a development environment out side of the cluster
// import _ "k8s.io/client-go/plugin/pkg/client/auth"
// import _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
22 changes: 13 additions & 9 deletions cmd/guard-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (

type config struct {
GuardServiceLogLevel string `split_words:"true" required:"false"`
GuardServicePort string `split_words:"true" required:"false"`
GuardServiceInterval string `split_words:"true" required:"false"`
}

Expand All @@ -65,19 +64,29 @@ func (l *learner) baseHandler(query url.Values) (record *serviceRecord, err erro
ns := utils.Sanitize(nsSlice[0])

if strings.HasPrefix(sid, "ns-") {
log.Infof("baseHandler illegal sid")
sid = ""
err = fmt.Errorf("illegal sid %s", sid)
return
}

if len(sid) < 1 {
err = fmt.Errorf("wrong sid %s", sidSlice[0])
return
}

if len(ns) < 1 {
err = fmt.Errorf("wrong ns %s", nsSlice[0])
return
}

// extract and sanitize cmFlag
var cmFlag bool
if len(cmFlagSlice) > 0 {
cmFlag = (cmFlagSlice[0] == "true")
}

// get session record, create one if does not exist
log.Debugf("** baseHandler ** ns %s, sid %s, cmFlag %t", ns, sid, cmFlag)
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
record = l.services.get(ns, sid, cmFlag)
if record == nil {
// should never happen
Expand Down Expand Up @@ -132,8 +141,6 @@ func (l *learner) processPile(w http.ResponseWriter, req *http.Request) {
}

func (l *learner) mainEventLoop(quit chan string) {
log.Infof("l.pileLearnTicker %v", l.pileLearnTicker)

for {
select {
case <-l.pileLearnTicker.Ch():
Expand Down Expand Up @@ -166,13 +173,10 @@ func preMain(minimumInterval time.Duration) (*learner, *http.ServeMux, string, c
mux.HandleFunc("/pile", l.processPile)

target := ":8888"
if env.GuardServicePort != "" {
target = fmt.Sprintf(":%s", env.GuardServicePort)
}

quit := make(chan string)

log.Infof("Starting guard-learner on %s", target)
log.Infof("Starting guard-service on %s", target)
return l, mux, target, quit
}

Expand All @@ -185,6 +189,6 @@ func main() {
go l.mainEventLoop(quit)

err := http.ListenAndServe(target, mux)
log.Infof("Failed to start %v", err)
log.Infof("Using target: %s - Failed to start %v", target, err)
quit <- "ListenAndServe failed"
}
9 changes: 0 additions & 9 deletions cmd/guard-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,6 @@ func TestFetchConfigHandler_main(t *testing.T) {
if target != ":8888" {
t.Errorf("handler returned wrong default target code: got %s want %s", target, ":8888")
}

os.Setenv("GUARD_SERVICE_PORT", "9999")
_, _, target, _ = preMain(utils.MinimumInterval)

if target != ":9999" {
t.Errorf("handler returned wrong default target code: got %s want %s", target, ":9999")
}

os.Unsetenv("GUARD_SERVICE_PORT")
}

func TestFetchConfigHandler_POST(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions cmd/queue/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"os"

_ "knative.dev/security-guard/pkg/guard-gate"
"knative.dev/security-guard/pkg/qpoption"
"knative.dev/serving/pkg/queue/sharedmain"
)

func main() {
qOpt := qpoption.NewGateQPOption()
defer qOpt.Shutdown()

if sharedmain.Main(qOpt.Setup) != nil {
qOpt.Shutdown()
os.Exit(1)
}
}
37 changes: 37 additions & 0 deletions config/deploy-guard-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: guard-service
labels:
app: guard-service
spec:
replicas: 1
selector:
matchLabels:
app: guard-service
template:
metadata:
labels:
app: guard-service
spec:
serviceAccountName: guard-service-account
imagePullSecrets:
- name: all-icr-io
containers:
- name: guard-service
image: ko://knative.dev/security-guard/cmd/guard-service
imagePullPolicy: Always
ports:
- containerPort: 8888
---
apiVersion: v1
kind: Service
metadata:
name: guard-service
spec:
selector:
app: guard-service
ports:
- protocol: TCP
port: 80
targetPort: 8888
27 changes: 27 additions & 0 deletions config/deploy-queue-proxy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2019 The Knative Authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to double-check that this doesn't overwrite the existing config-deployment... might be better to just have the setup-guard-service script patch the existing configmap with the current QP image...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested by adding a test: boom parameter to config.deployment - this parameter remains when doing security-guard ko apply -Rf ./config while the image changes - note that the _example is removed after such a change.

after ko apply -Rf config/core at serving

data:
  _example: |-
    ################################
    #                              #
    #    EXAMPLE CONFIGURATION     #
    #                              #
    ################################

    # This block is not actually functional configuration,
    ...
    # NOTE THAT THIS IS AN EXPERIMENTAL / ALPHA FEATURE
    concurrency-state-endpoint: ""
  queueSidecarImage: us.icr.io/knat/queue-39be6f1d08a095bd076a71d288d295b6@sha256:086b8a7d8bc2104672609a1918274f2e4b075076f52c0fbbc4c3f044b64eb5fe
  test: boom

after ko apply -Rf ./config at security-guard

data:
  queue-sidecar-image: us.icr.io/knat/queue-958f2fa0da7e00e1e412a537ecf9d1cc@sha256:021e6438111346ea5d5aff064d6c3ad2cea6c3a92cc7e7708f644f8a1fc34a1e
  test: boom

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-deployment
namespace: knative-serving
labels:
app.kubernetes.io/name: knative-serving
app.kubernetes.io/component: controller
app.kubernetes.io/version: devel
data:
# This overrides the configmap produced by knative serving
# It is useful for demonstration purposes or when using an unchanged configmap
queue-sidecar-image: ko://knative.dev/security-guard/cmd/queue
File renamed without changes.
File renamed without changes.
10 changes: 4 additions & 6 deletions deploy/serviceAccount.yaml → config/serviceAccount.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: guardian-cluster-role
name: guardian-service
labels:
rbac.authorization.k8s.io/guardian: 'true'
rules:
Expand All @@ -22,17 +22,15 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: guard-service-account
namespace: knative-guard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
kind: RoleBinding
metadata:
name: guardian-admin
subjects:
- kind: ServiceAccount
name: guard-service-account
namespace: knative-guard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: guardian-cluster-role
name: guardian-service
6 changes: 6 additions & 0 deletions hack/setup-guard-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kubectl apply -f config/gateAccount.yaml
kubectl apply -f config/serviceAccount.yaml
kubectl apply -f config/guardiansCrd.yaml
davidhadas marked this conversation as resolved.
Show resolved Hide resolved

./hack/update-guard-service.sh

2 changes: 2 additions & 0 deletions hack/update-guard-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export KO_DOCKER_REPO=us.icr.io/knat
davidhadas marked this conversation as resolved.
Show resolved Hide resolved
ko apply -Rf ./config/deploy-guard-service.yaml
davidhadas marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 7 additions & 8 deletions pkg/guard-gate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type httpClientInterface interface {
}

type httpClient struct {
client *http.Client
client http.Client
}

func (hc *httpClient) Do(req *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -73,19 +73,17 @@ func (srv *gateClient) reportPile() {
}
defer srv.clearPile()

pi.Log.Infof("Reporting a pile with pileCount %d records to guard-service", srv.pile.Count)

postBody, marshalErr := json.Marshal(srv.pile)

if marshalErr != nil {
// should never happen
pi.Log.Warnf("Error during marshal: %v", marshalErr)
pi.Log.Infof("Error during marshal: %v", marshalErr)
return
}
reqBody := bytes.NewBuffer(postBody)
req, err := http.NewRequest(http.MethodPost, srv.guardServiceUrl+"/pile", reqBody)
if err != nil {
pi.Log.Warnf("Http.NewRequest error %v", err)
pi.Log.Infof("Http.NewRequest error %v", err)
return
}
query := req.URL.Query()
Expand All @@ -95,10 +93,11 @@ func (srv *gateClient) reportPile() {
query.Add("cm", "true")
}
req.URL.RawQuery = query.Encode()
pi.Log.Infof("Reporting a pile with pileCount %d records to guard-service", srv.pile.Count)

res, postErr := srv.httpClient.Do(req)
if postErr != nil {
pi.Log.Warnf("httpClient.Do error %v", postErr)
pi.Log.Infof("httpClient.Do error %v", postErr)
return
}
if res.Body != nil {
Expand Down Expand Up @@ -135,7 +134,7 @@ func (srv *gateClient) loadGuardian() *spec.GuardianSpec {
func (srv *gateClient) loadGuardianFromService() *spec.GuardianSpec {
req, err := http.NewRequest(http.MethodGet, srv.guardServiceUrl+"/config", nil)
if err != nil {
pi.Log.Warnf("loadGuardianFromService Http.NewRequest error %v", err)
pi.Log.Infof("loadGuardianFromService Http.NewRequest error %v", err)
return nil
}
query := req.URL.Query()
Expand All @@ -148,7 +147,7 @@ func (srv *gateClient) loadGuardianFromService() *spec.GuardianSpec {

res, err := srv.httpClient.Do(req)
if err != nil {
pi.Log.Warnf("loadGuardianFromService httpClient.Do error %v", err)
pi.Log.Infof("loadGuardianFromService httpClient.Do error %v", err)
return nil
}

Expand Down
Loading