Skip to content

Commit

Permalink
Improved dev environment (oauth2-proxy#2211)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Create launch.json

* Update .devcontainer/Dockerfile

* Apply suggestions from code review

---------

Co-authored-by: Jan Larwig <[email protected]>
  • Loading branch information
kvanzuijlen and tuunit authored Jan 20, 2024
1 parent b8ae644 commit f88f09f
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ c.out
_obj
_test
.idea/
.vscode/
.vscode/*
!/.vscode/tasks.json

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
76 changes: 76 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
32 changes: 32 additions & 0 deletions pkg/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"os"

"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
. "github.com/onsi/ginkgo"
Expand All @@ -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())))
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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,
}),
)
})
Expand Down Expand Up @@ -738,6 +761,7 @@ var _ = Describe("Server", func() {
var listenAddr string

BeforeEach(func() {
skipDevContainer()
var err error
srv, err = NewServer(Opts{
Handler: handler,
Expand Down Expand Up @@ -788,6 +812,7 @@ var _ = Describe("Server", func() {
var secureListenAddr string

BeforeEach(func() {
skipDevContainer()
var err error
srv, err = NewServer(Opts{
Handler: handler,
Expand Down Expand Up @@ -857,6 +882,7 @@ var _ = Describe("Server", func() {
var listenAddr, secureListenAddr string

BeforeEach(func() {
skipDevContainer()
var err error
srv, err = NewServer(Opts{
Handler: handler,
Expand Down Expand Up @@ -965,3 +991,9 @@ var _ = Describe("Server", func() {
)
})
})

func skipDevContainer() {
if os.Getenv("DEVCONTAINER") != "" {
Skip("Skipping testing in DevContainer environment")
}
}

0 comments on commit f88f09f

Please sign in to comment.