Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Packages - moving the build into packages #248

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ FEATURES
* updated the base image to apline 3.6 in commit [0fdebaf821](https://github.com/gambol99/keycloak-proxy/pull/236/commits/0fdebaf8215e9480896f01ec7ab2ef7caa242da1)
* moved to use zap for the logging [#PR237](https://github.com/gambol99/keycloak-proxy/pull/237)
* making the X-Auth-Token optional in the upstream headers via the --enable-token-header [#PR247](https://github.com/gambol99/keycloak-proxy/pull/247)
* the upstream url is optional, meaning when not configured via --upstream-url is will proxy all requests to the Host header [#PR248](https://github.com/gambol99/keycloak-proxy/pull/248)
* updated the Dockerfile to use stages and build in one go [#PR?](https://github.com/gambol99/keycloak-proxy/pull/?]
* adding the ability to load a CA authority to provide trust on upstream endpoint [#PR248](https://github.com/gambol99/keycloak-proxy/pull/248)

BREAKING CHANGES:
Expand Down
22 changes: 15 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
FROM alpine:3.6
MAINTAINER Rohith Jayawardene <[email protected]>
FROM golang:1.8 as build
RUN go get -d github.com/gambol99/keycloak-proxy \
&& cd /go/src/github.com/gambol99/keycloak-proxy \
&& make static

FROM ubuntu as certs
RUN apt-get update && apt-get install -y ca-certificates

FROM scratch
COPY --from=build /go/src/github.com/gambol99/keycloak-proxy/bin/keycloak-proxy /opt/keycloak-proxy
COPY --from=certs /etc/ssl/certs /etc/ssl/certs

LABEL Name=keycloak-proxy \
Maintainer="Rohith Jayawardene <[email protected]>" \
Release=https://github.com/gambol99/keycloak-proxy \
Url=https://github.com/gambol99/keycloak-proxy \
Help=https://github.com/gambol99/keycloak-proxy/issues

RUN apk add ca-certificates --update

ADD templates/ /opt/templates
ADD bin/keycloak-proxy /opt/keycloak-proxy

WORKDIR "/opt"
WORKDIR /opt

ENTRYPOINT [ "/opt/keycloak-proxy" ]
CMD [ "/opt/keycloak-proxy" ]
34 changes: 16 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ ROOT_DIR=${PWD}
HARDWARE=$(shell uname -m)
GIT_SHA=$(shell git --no-pager describe --always --dirty)
BUILD_TIME=$(shell date '+%s')
VERSION ?= $(shell awk '/release.*=/ { print $$3 }' doc.go | sed 's/"//g')
VERSION ?= $(shell awk '/Release.*=/ { print $$3 }' pkg/constants/const.go | sed 's/"//g')
DEPS=$(shell go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)
PACKAGES=$(shell go list ./...)
LFLAGS ?= -X main.gitsha=${GIT_SHA} -X main.compiled=${BUILD_TIME}
VETARGS ?= -asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr
PACKAGES=$(shell go list ./... | grep -v vendor)
LFLAGS ?= -X constants.Gitsha=${GIT_SHA} -X constants.Compiled=${BUILD_TIME}

.PHONY: test authors changelog build docker static release lint cover vet glide-install

Expand All @@ -24,7 +23,7 @@ golang:
build: golang
@echo "--> Compiling the project"
@mkdir -p bin
go build -ldflags "${LFLAGS}" -o bin/${NAME}
go build -ldflags "${LFLAGS}" -o bin/${NAME} cmd/keycloak-proxy/*.go

static: golang deps
@echo "--> Compiling the static binary"
Expand All @@ -39,9 +38,9 @@ docker-build:
-e GOOS=linux golang:${GOVERSION} \
make static

docker-test:
docker-test: static docker
@echo "--> Running the docker test"
docker run --rm -ti -p 3000:3000 \
docker run --rm -ti --net=host \
-v ${ROOT_DIR}/config.yml:/etc/keycloak/config.yml:ro \
-v ${ROOT_DIR}/tests:/opt/tests:ro \
${REGISTRY}/${AUTHOR}/${NAME}:${VERSION} --config /etc/keycloak/config.yml
Expand Down Expand Up @@ -94,7 +93,7 @@ vet:
@go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \
go get golang.org/x/tools/cmd/vet; \
fi
@go tool vet $(VETARGS) *.go
@go vet $(PACKAGES)

lint:
@echo "--> Running golint"
Expand All @@ -105,12 +104,11 @@ lint:

gofmt:
@echo "--> Running gofmt check"
@gofmt -s -l *.go \
| grep -q \.go ; if [ $$? -eq 0 ]; then \
echo "You need to runn the make format, we have file unformatted"; \
gofmt -s -l *.go; \
exit 1; \
fi
@gofmt -s -l *.go | grep -q \.go ; if [ $$? -eq 0 ]; then \
echo "You need to runn the make format, we have file unformatted"; \
gofmt -s -l *.go; \
exit 1; \
fi

verify:
@echo "--> Verifying the code"
Expand All @@ -127,26 +125,26 @@ bench:
coverage:
@echo "--> Running go coverage"
@go test -coverprofile cover.out
@go tool cover -html=cover.out -o cover.html
@go tool cover $(PACKAGES) -html=cover.out -o cover.html

cover:
@echo "--> Running go cover"
@go test --cover
@go test --cover $(PACKAGES)

test:
@echo "--> Running the tests"
@if [ ! -d "vendor" ]; then \
make glide-install; \
fi
@go test -v
@go test -v $(PACKAGES)
@$(MAKE) golang
@$(MAKE) gofmt
@$(MAKE) vet
@$(MAKE) cover

all: test
echo "--> Performing all tests"
@${MAKE} verify
@$(MAKE) verify
@$(MAKE) bench
@$(MAKE) coverage

Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ Note, anything defined in the configuration file can also be configured as comma

```shell
bin/keycloak-proxy \
--discovery-url=https://keycloak.example.com/auth/realms/<REALM_NAME> \
--client-id=<CLIENT_ID> \
--client-secret=<SECRET> \
--listen=127.0.0.1:3000 \ # unix sockets format unix://path
--redirection-url=http://127.0.0.1:3000 \
--enable-refresh-token=true \
--encryption-key=AgXa7xRcoClDEU0ZDSH4X0XhL5Qy2Z2j \
--upstream-url=http://127.0.0.1:80 \
--resources="uri=/admin*|methods=GET|roles=test1,test2" \
--resources="uri=/backend*|roles=test1"
--discovery-url=https://keycloak.example.com/auth/realms/<REALM_NAME> \
--client-id=<CLIENT_ID> \
--client-secret=<SECRET> \
--listen=127.0.0.1:3000 \ # unix sockets format unix://path
--redirection-url=http://127.0.0.1:3000 \
--enable-refresh-token=true \
--encryption-key=AgXa7xRcoClDEU0ZDSH4X0XhL5Qy2Z2j \
--upstream-url=http://127.0.0.1:80 \
--resources="uri=/admin*|methods=GET|roles=test1,test2" \
--resources="uri=/backend*|roles=test1"
```

#### **HTTP Routing**
Expand Down
File renamed without changes.
61 changes: 35 additions & 26 deletions cli.go → cmd/keycloak-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,27 @@ import (
"syscall"
"time"

"github.com/gambol99/keycloak-proxy/pkg/api"
"github.com/gambol99/keycloak-proxy/pkg/constants"
"github.com/gambol99/keycloak-proxy/pkg/server"
"github.com/gambol99/keycloak-proxy/pkg/utils"

"github.com/urfave/cli"
)

const (
envPrefix = "PROXY_"
)

// newOauthProxyApp creates a new cli application and runs it
func newOauthProxyApp() *cli.App {
config := newDefaultConfig()
config := api.NewDefaultConfig()
app := cli.NewApp()
app.Name = prog
app.Usage = description
app.Version = getVersion()
app.Author = author
app.Email = email
app.Name = constants.Prog
app.Usage = constants.Description
app.Version = constants.GetVersion()
app.Author = constants.Author
app.Email = constants.Email
app.Flags = getCommandLineOptions()
app.UsageText = "keycloak-proxy [options]"

Expand All @@ -49,30 +58,30 @@ func newOauthProxyApp() *cli.App {
configFile := cx.String("config")
// step: do we have a configuration file?
if configFile != "" {
if err := readConfigFile(configFile, config); err != nil {
return printError("unable to read the configuration file: %s, error: %s", configFile, err.Error())
if err := utils.ReadConfigFile(configFile, config); err != nil {
return utils.PrintError("unable to read the configuration file: %s, error: %s", configFile, err.Error())
}
}

// step: parse the command line options
if err := parseCLIOptions(cx, config); err != nil {
return printError(err.Error())
return utils.PrintError(err.Error())
}

// step: validate the configuration
if err := config.isValid(); err != nil {
return printError(err.Error())
if err := config.IsValid(); err != nil {
return utils.PrintError(err.Error())
}

// step: create the proxy
proxy, err := newProxy(config)
proxy, err := server.New(config)
if err != nil {
return printError(err.Error())
return utils.PrintError(err.Error())
}

// step: start the service
if err := proxy.Run(); err != nil {
return printError(err.Error())
return utils.PrintError(err.Error())
}

// step: setup the termination signals
Expand All @@ -89,11 +98,11 @@ func newOauthProxyApp() *cli.App {
// getCommandLineOptions builds the command line options by reflecting the Config struct and extracting
// the tagged information
func getCommandLineOptions() []cli.Flag {
defaults := newDefaultConfig()
defaults := api.NewDefaultConfig()
var flags []cli.Flag
count := reflect.TypeOf(Config{}).NumField()
count := reflect.TypeOf(api.Config{}).NumField()
for i := 0; i < count; i++ {
field := reflect.TypeOf(Config{}).Field(i)
field := reflect.TypeOf(api.Config{}).Field(i)
usage, found := field.Tag.Lookup("usage")
if !found {
continue
Expand Down Expand Up @@ -150,15 +159,15 @@ func getCommandLineOptions() []cli.Flag {
}

// parseCLIOptions parses the command line options and constructs a config object
func parseCLIOptions(cx *cli.Context, config *Config) (err error) {
func parseCLIOptions(cx *cli.Context, config *api.Config) (err error) {
// step: we can ignore these options in the Config struct
ignoredOptions := []string{"tag-data", "match-claims", "resources", "headers"}
// step: iterate the Config and grab command line options via reflection
count := reflect.TypeOf(config).Elem().NumField()
for i := 0; i < count; i++ {
field := reflect.TypeOf(config).Elem().Field(i)
name := field.Tag.Get("yaml")
if containedIn(name, ignoredOptions) {
if utils.ContainedIn(name, ignoredOptions) {
continue
}

Expand All @@ -181,29 +190,29 @@ func parseCLIOptions(cx *cli.Context, config *Config) (err error) {
}
}
if cx.IsSet("tag") {
tags, err := decodeKeyPairs(cx.StringSlice("tag"))
tags, err := utils.DecodeKeyPairs(cx.StringSlice("tag"))
if err != nil {
return err
}
mergeMaps(config.Tags, tags)
utils.MergeMaps(config.Tags, tags)
}
if cx.IsSet("match-claims") {
claims, err := decodeKeyPairs(cx.StringSlice("match-claims"))
claims, err := utils.DecodeKeyPairs(cx.StringSlice("match-claims"))
if err != nil {
return err
}
mergeMaps(config.MatchClaims, claims)
utils.MergeMaps(config.MatchClaims, claims)
}
if cx.IsSet("headers") {
headers, err := decodeKeyPairs(cx.StringSlice("headers"))
headers, err := utils.DecodeKeyPairs(cx.StringSlice("headers"))
if err != nil {
return err
}
mergeMaps(config.Headers, headers)
utils.MergeMaps(config.Headers, headers)
}
if cx.IsSet("resources") {
for _, x := range cx.StringSlice("resources") {
resource, err := newResource().parse(x)
resource, err := api.NewResource().Parse(x)
if err != nil {
return fmt.Errorf("invalid resource %s, %s", x, err)
}
Expand Down
4 changes: 3 additions & 1 deletion cli_test.go → cmd/keycloak-proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package main
import (
"testing"

"github.com/gambol99/keycloak-proxy/pkg/api"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
Expand All @@ -37,7 +39,7 @@ func TestReadOptions(t *testing.T) {
c := cli.NewApp()
c.Flags = getCommandLineOptions()
c.Action = func(cx *cli.Context) error {
parseCLIOptions(cx, &Config{})
parseCLIOptions(cx, &api.Config{})
return nil
}
c.Run([]string{""})
Expand Down
Loading