From f88f09f962264c4073f343b901c58bcc9953411e Mon Sep 17 00:00:00 2001 From: Koen van Zuijlen <8818390+kvanzuijlen@users.noreply.github.com> Date: Sat, 20 Jan 2024 21:10:37 +0100 Subject: [PATCH] Improved dev environment (#2211) * Improved dev env setup * Cleanup duplicate checks * Applied PR feedback * Updated go.mod/go.sum * go mod tidy * Update .devcontainer/devcontainer.json * Update pkg/http/server_test.go Co-authored-by: Jan Larwig * Create launch.json * Update .devcontainer/Dockerfile * Apply suggestions from code review --------- Co-authored-by: Jan Larwig --- .devcontainer/Dockerfile | 14 ++++++ .devcontainer/devcontainer.json | 45 +++++++++++++++++++ .gitignore | 3 +- .pre-commit-config.yaml | 13 ++++++ .vscode/launch.json | 36 ++++++++++++++++ .vscode/tasks.json | 76 +++++++++++++++++++++++++++++++++ pkg/http/server_test.go | 32 ++++++++++++++ 7 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .pre-commit-config.yaml create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000000..a0cb025652 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,14 @@ +FROM mcr.microsoft.com/vscode/devcontainers/go:0-1.19 + +SHELL ["/bin/bash", "-o", "pipefail", "-c"] + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + pre-commit \ + vim \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* \ + && mkdir -p /go/pkg \ + && chmod -R a+w /go/pkg + +WORKDIR /workspace diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000000..0d2ffecc2a --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,45 @@ +{ + "name": "oauth2-proxy Dev", + "context": "..", + "dockerFile": "./Dockerfile", + "postCreateCommand": "pre-commit install && go mod download", + "containerEnv": { "DEVCONTAINER": "1" }, + "appPort": ["4180:4180"], + "runArgs": ["-e", "GIT_EDITOR=code --wait"], + "customizations": { + "vscode": { + "extensions": [ + "golang.go", + "ms-vscode.makefile-tools", + "visualstudioexptteam.vscodeintellicode", + "redhat.vscode-yaml", + "esbenp.prettier-vscode", + "GitHub.vscode-pull-request-github" + ], + "settings": { + "editor.formatOnPaste": false, + "editor.formatOnSave": true, + "editor.formatOnType": true, + "files.trimTrailingWhitespace": true, + "yaml.customTags": [ + "!input scalar", + "!secret scalar", + "!include_dir_named scalar", + "!include_dir_list scalar", + "!include_dir_merge_list scalar", + "!include_dir_merge_named scalar" + ] + } + } + }, + "features": { + "ghcr.io/devcontainers/features/docker-in-docker": {}, + "ghcr.io/devcontainers/features/common-utils": { + "configureZshAsDefaultShell": true, + "username": "vscode", + "userUid": 1000, + "userGid": 1000 + }, + "ghcr.io/devcontainers/features/kubectl-helm-minikube": {} + } +} diff --git a/.gitignore b/.gitignore index 57f3044462..e40acd5217 100644 --- a/.gitignore +++ b/.gitignore @@ -18,7 +18,8 @@ c.out _obj _test .idea/ -.vscode/ +.vscode/* +!/.vscode/tasks.json # Architecture specific extensions/prefixes *.[568vq] diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..95c00d1bf4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,13 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/dnephin/pre-commit-golang + rev: v0.5.0 + hooks: + - id: golangci-lint + - id: go-build diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000000..36c7fb09bd --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,36 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "OAuth2 Proxy for Dex", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "args": [ + "--config", "contrib/local-environment/oauth2-proxy.cfg" + ] + }, + { + "name": "OAuth2 Proxy for Keycloak", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "args": [ + "--config", "contrib/local-environment/oauth2-proxy-keycloak.cfg" + ] + }, + { + "name": "OAuth2 Proxy with Alpha Config", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "args": [ + "--config", "contrib/local-environment/oauth2-proxy-alpha-config.cfg", + "--alpha-config", "contrib/local-environment/oauth2-proxy-alpha-config.yaml" + ] + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000000..0141101771 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,76 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Run oauth2-proxy", + "type": "shell", + "command": "./oauth2-proxy --config contrib/local-environment/oauth2-proxy.cfg --alpha-config contrib/local-environment/oauth2-proxy-alpha0config.cfg", + "dependsOn": ["Build"], + "group": { + "kind": "test", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "problemMatcher": [] + }, + { + "label": "Test", + "type": "shell", + "command": "make test", + "group": { + "kind": "test", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "problemMatcher": [] + }, + { + "label": "Ruff", + "type": "shell", + "command": "pre-commit run ruff --all-files", + "group": { + "kind": "test", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "problemMatcher": [] + }, + { + "label": "golint", + "type": "shell", + "command": "make lint", + "group": { + "kind": "test", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "problemMatcher": [] + }, + { + "label": "Build", + "type": "shell", + "command": "make build", + "group": { + "kind": "test", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "problemMatcher": [] + } + ] +} diff --git a/pkg/http/server_test.go b/pkg/http/server_test.go index dac49601cc..4d4ab86056 100644 --- a/pkg/http/server_test.go +++ b/pkg/http/server_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "os" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options" . "github.com/onsi/ginkgo" @@ -26,9 +27,13 @@ var _ = Describe("Server", func() { expectedErr error expectHTTPListener bool expectTLSListener bool + ipv6 bool } DescribeTable("When creating the new server from the options", func(in *newServerTableInput) { + if in.ipv6 { + skipDevContainer() + } srv, err := NewServer(in.opts) if in.expectedErr != nil { Expect(err).To(MatchError(ContainSubstring(in.expectedErr.Error()))) @@ -303,6 +308,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: true, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 valid https bind address, with no TLS config", &newServerTableInput{ opts: Opts{ @@ -312,6 +318,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: no TLS config provided"), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 valid https bind address, and valid TLS config", &newServerTableInput{ opts: Opts{ @@ -325,6 +332,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), Entry("with a both a ipv6 valid http and ipv6 valid https bind address, and valid TLS config", &newServerTableInput{ opts: Opts{ @@ -339,6 +347,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: true, expectTLSListener: true, + ipv6: true, }), Entry("with an ipv6 invalid bind address scheme", &newServerTableInput{ opts: Opts{ @@ -348,6 +357,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up listener: listen (invalid, [::1]:0) failed: listen invalid: unknown network invalid"), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 invalid secure bind address scheme", &newServerTableInput{ opts: Opts{ @@ -361,6 +371,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), Entry("with an ipv6 invalid bind address port", &newServerTableInput{ opts: Opts{ @@ -370,6 +381,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up listener: listen (tcp, [::1]:a) failed: listen tcp: "), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 invalid secure bind address port", &newServerTableInput{ opts: Opts{ @@ -383,6 +395,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: listen ([::1]:a) failed: listen tcp: "), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 invalid TLS key", &newServerTableInput{ opts: Opts{ @@ -398,6 +411,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: could not load certificate: could not parse certificate data: tls: failed to find any PEM data in key input"), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 invalid TLS cert", &newServerTableInput{ opts: Opts{ @@ -413,6 +427,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: could not load certificate: could not parse certificate data: tls: failed to find any PEM data in certificate input"), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 address, with no TLS key", &newServerTableInput{ opts: Opts{ @@ -425,6 +440,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: could not load certificate: could not load key data: no configuration provided"), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("with an ipv6 address, with no TLS cert", &newServerTableInput{ opts: Opts{ @@ -437,6 +453,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: could not load certificate: could not load cert data: no configuration provided"), expectHTTPListener: false, expectTLSListener: false, + ipv6: true, }), Entry("when the ipv6 bind address is prefixed with the http scheme", &newServerTableInput{ opts: Opts{ @@ -446,6 +463,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: true, expectTLSListener: false, + ipv6: true, }), Entry("when the ipv6 secure bind address is prefixed with the https scheme", &newServerTableInput{ opts: Opts{ @@ -459,6 +477,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), Entry("with an ipv6 valid https bind address, and valid TLS config with MinVersion", &newServerTableInput{ opts: Opts{ @@ -473,6 +492,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), Entry("with an ipv6 valid https bind address, and invalid TLS config with unknown MinVersion", &newServerTableInput{ opts: Opts{ @@ -487,6 +507,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: unknown TLS MinVersion config provided"), expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), Entry("with an ipv6 valid https bind address, and valid TLS config with CipherSuites", &newServerTableInput{ opts: Opts{ @@ -504,6 +525,7 @@ var _ = Describe("Server", func() { expectedErr: nil, expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), Entry("with an ipv6 valid https bind address, and invalid TLS config with unknown CipherSuites", &newServerTableInput{ opts: Opts{ @@ -521,6 +543,7 @@ var _ = Describe("Server", func() { expectedErr: errors.New("error setting up TLS listener: could not parse cipher suites: unknown TLS cipher suite name specified \"TLS_RSA_WITH_RC4_64_SHA\""), expectHTTPListener: false, expectTLSListener: true, + ipv6: true, }), ) }) @@ -738,6 +761,7 @@ var _ = Describe("Server", func() { var listenAddr string BeforeEach(func() { + skipDevContainer() var err error srv, err = NewServer(Opts{ Handler: handler, @@ -788,6 +812,7 @@ var _ = Describe("Server", func() { var secureListenAddr string BeforeEach(func() { + skipDevContainer() var err error srv, err = NewServer(Opts{ Handler: handler, @@ -857,6 +882,7 @@ var _ = Describe("Server", func() { var listenAddr, secureListenAddr string BeforeEach(func() { + skipDevContainer() var err error srv, err = NewServer(Opts{ Handler: handler, @@ -965,3 +991,9 @@ var _ = Describe("Server", func() { ) }) }) + +func skipDevContainer() { + if os.Getenv("DEVCONTAINER") != "" { + Skip("Skipping testing in DevContainer environment") + } +}