Skip to content

Commit d9c1590

Browse files
author
Jeremy Lewi
committed
Create the scaffolding for a simple webserver to fulfill dialogflow requests.
* This is intended to be a simple web server that will answer queries about who owns which GitHub labels. * This PR is just the scaffolding for the server * The server provides an endpoint for the Dialogflow webhook but the endpoint isn't actually returning valid responses yet. Related to kubeflow#142
1 parent 17c8608 commit d9c1590

File tree

18 files changed

+1094
-0
lines changed

18 files changed

+1094
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fairing/__pycache__/**
1414
*.pyc
1515
py/code_intelligence/.data/**
1616

17+
**/.idea
1718
# ignore coredumps
1819
**/core.*
1920
# ignore checkpoints

chatbot/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build the manager binary
2+
ARG GOLANG_VERSION=1.13.1
3+
FROM golang:${GOLANG_VERSION} as builder
4+
5+
WORKDIR /workspace
6+
# Copy the Go Modules manifests
7+
COPY go.mod go.mod
8+
COPY go.sum go.sum
9+
# cache deps before building and copying source so that we don't need to re-download as much
10+
# and so that source changes don't invalidate our downloaded layer
11+
RUN go mod download
12+
13+
# Copy the go source
14+
COPY . ./
15+
16+
# Build
17+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o server ./cmd/main.go
18+
19+
# Use distroless as minimal base image to package the manager binary
20+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
21+
FROM gcr.io/distroless/base:latest as serve
22+
WORKDIR /
23+
COPY --from=builder /workspace/server /server
24+
25+
EXPOSE 8080
26+
27+
ENTRYPOINT ["/server"]

chatbot/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Chatbot
2+
3+
This is a Dialogflow fulfillment server.
4+
5+
## Deployment
6+
7+
It is currently running in
8+
9+
* **Full cluster name**: gke_issue-label-bot-dev_us-east1-d_issue-label-bot
10+
* **project**: issue-label-bot-dev
11+
* **cluster**: issue-label-bot
12+
13+
## Notes.
14+
15+
To expose the webhook we need to bypass IAP. To do this we create a second K8s service to create a second GCP Backend Service
16+
but with IAP enabled.
17+
18+
```
19+
kubectl --context=issue-label-bot-dev -n istio-system create -f istio-ingressgateway.yaml
20+
```
21+
22+
We need to modify the security policy applied at the ingress gateway so that it won't reject requests without a valid
23+
JWT.
24+
25+
To deploy the fullfilment server we need to modify the Kubeflow ingress policy to allow traffic from the dialgoflow webserver.
26+
This traffic can't be routed through IAP. We will still use a JWT to restrict traffic but it will be a JWT we create.
27+
28+
So we need to add a second JWT origin rule to match this traffic to the policy.
29+
30+
We can do this as
31+
32+
```
33+
kubectl --context=issue-label-bot-dev -n istio-system patch policy ingress-jwt -p "$(cat ingress-jwt.patch.yaml)" --type=merge
34+
```
35+
36+
To verify that is working we can port-forward to the service.
37+
38+
```
39+
kubectl --context=issue-label-bot-dev -n istio-system port-forward service/chatbot-istio-ingressgateway 9080:80
40+
```
41+
42+
Send a request with a JWT this should fail with "Origin Authentication Failure" since there is no JWT.
43+
44+
```
45+
curl localhost:9080/chatbot/dev/ -d '{}' -H "Content-Type: application/json"
46+
```
47+
48+
49+
50+
To authorize Dialogflow webhook we will use a JWT. We use the jose-util to generate a public private key pair
51+
52+
```
53+
git clone [email protected]:square/go-jose.git git_go-jose
54+
cd git_go-jose/jose-uitl
55+
go build.
56+
```
57+
58+
Generate a key pair
59+
60+
61+
```
62+
./jose-util generate-key --alg=ES256 --use sig --kid=chatbot
63+
```
64+
65+
Upload the public bit to a public GCS bucket
66+
67+
```
68+
https://storage.cloud.google.com/issue-label-bot-dev_public/chatbot/keys/jwk-sig-chatbot-pub.json
69+
```
70+
71+
## Referencess
72+
73+
* [ISTIO 1.1 Policy Resource](https://archive.istio.io/v1.1/docs/reference/config/istio.authentication.v1alpha1/#Policy)
74+
* [ISTIO 1.5 JWT policy example](https://istio.io/docs/tasks/security/authorization/authz-jwt/)
75+
* This example includes some static JWTs that can be used for testing.

chatbot/cmd/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"github.com/kubeflow/code-intelligence/chatbot/cmd/options"
6+
"github.com/kubeflow/code-intelligence/chatbot/pkg"
7+
"github.com/onrik/logrus/filename"
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
func init() {
12+
// Add filename as one of the fields of the structured log message
13+
filenameHook := filename.NewHook()
14+
filenameHook.Field = "filename"
15+
log.AddHook(filenameHook)
16+
}
17+
18+
// Run the application.
19+
func Run(opt *options.ServerOption) error {
20+
log.Info("Creating server")
21+
server, err := pkg.NewKubeflowInfoServer(opt.AreaConfigPath)
22+
if err != nil {
23+
return err
24+
}
25+
26+
27+
server.RegisterEndpoints()
28+
29+
log.Infof("Starting http server.")
30+
return server.StartHttp(opt.Port)
31+
}
32+
33+
func main() {
34+
s := options.NewServerOption()
35+
s.AddFlags(flag.CommandLine)
36+
37+
flag.Parse()
38+
39+
if s.AreaConfigPath == "" {
40+
log.Fatalf("--area-config-path must be specified. This should be the path to a YAML file defining the areas and their associated owners")
41+
}
42+
if s.JsonLogFormat {
43+
// Output logs in a json format so that it can be parsed by services like Stackdriver
44+
log.SetFormatter(&log.JSONFormatter{})
45+
}
46+
if err := Run(s); err != nil {
47+
log.Fatalf("%v\n", err)
48+
}
49+
}

chatbot/cmd/options/options.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// Copyright 2018 The Kubeflow Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package options
17+
18+
import (
19+
"flag"
20+
)
21+
22+
// ServerOption is the main context object for terver.
23+
type ServerOption struct {
24+
PrintVersion bool
25+
JsonLogFormat bool
26+
AreaConfigPath string
27+
Port int
28+
}
29+
30+
// NewServerOption creates a new CMServer with a default config.
31+
func NewServerOption() *ServerOption {
32+
s := ServerOption{}
33+
return &s
34+
}
35+
36+
// AddFlags adds flags for a specific Server to the specified FlagSet
37+
func (s *ServerOption) AddFlags(fs *flag.FlagSet) {
38+
fs.BoolVar(&s.JsonLogFormat, "json-log-format", true, "Set true to use json style log format. Set false to use plaintext style log format")
39+
fs.StringVar(&s.AreaConfigPath, "area-config-path", "https://raw.githubusercontent.com/kubeflow/community/master/labels-owners.yaml", "Path to the YAML file mapping area labels to owners.")
40+
fs.IntVar(&s.Port, "port", 8080, "The port to use for an http server.")
41+
}

chatbot/go.mod

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/kubeflow/code-intelligence/chatbot
2+
3+
go 1.13
4+
5+
require (
6+
github.com/ghodss/yaml v1.0.0
7+
github.com/go-kit/kit v0.9.0
8+
github.com/hashicorp/go-getter v1.4.1
9+
github.com/onrik/logrus v0.5.1
10+
github.com/pkg/errors v0.9.1
11+
github.com/prometheus/client_golang v1.6.0
12+
github.com/sirupsen/logrus v1.6.0
13+
github.com/square/go-jose/v3 v3.0.0-20200430180204-d84c719419c2
14+
github.com/tidwall/gjson v1.6.0 // indirect
15+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
16+
gopkg.in/yaml.v2 v2.2.8 // indirect
17+
)

0 commit comments

Comments
 (0)