From 44a2b85dba5b4afe3a24378c10e7e062cdb7f155 Mon Sep 17 00:00:00 2001 From: Antoine <13622487+skwair@users.noreply.github.com> Date: Fri, 9 Sep 2022 12:46:09 +0200 Subject: [PATCH] Display default TLS options in the dashboard --- pkg/api/handler_http.go | 5 ++ pkg/api/handler_http_test.go | 47 +++++++++++++++++++ .../router-baz-custom-tls-options.json | 20 ++++++++ .../router-baz-default-tls-options.json | 20 ++++++++ 4 files changed, 92 insertions(+) create mode 100644 pkg/api/testdata/router-baz-custom-tls-options.json create mode 100644 pkg/api/testdata/router-baz-default-tls-options.json diff --git a/pkg/api/handler_http.go b/pkg/api/handler_http.go index b566e7c61..b2643cdfa 100644 --- a/pkg/api/handler_http.go +++ b/pkg/api/handler_http.go @@ -11,6 +11,7 @@ import ( "github.com/gorilla/mux" "github.com/traefik/traefik/v2/pkg/config/runtime" "github.com/traefik/traefik/v2/pkg/log" + "github.com/traefik/traefik/v2/pkg/tls" ) type routerRepresentation struct { @@ -20,6 +21,10 @@ type routerRepresentation struct { } func newRouterRepresentation(name string, rt *runtime.RouterInfo) routerRepresentation { + if rt.TLS != nil && rt.TLS.Options == "" { + rt.TLS.Options = tls.DefaultTLSConfigName + } + return routerRepresentation{ RouterInfo: rt, Name: name, diff --git a/pkg/api/handler_http_test.go b/pkg/api/handler_http_test.go index e3698fa4c..f6137f202 100644 --- a/pkg/api/handler_http_test.go +++ b/pkg/api/handler_http_test.go @@ -223,6 +223,52 @@ func TestHandler_HTTP(t *testing.T) { jsonFile: "testdata/router-bar.json", }, }, + { + desc: "one router by id, implicitly using default TLS options", + path: "/api/http/routers/baz@myprovider", + conf: runtime.Configuration{ + Routers: map[string]*runtime.RouterInfo{ + "baz@myprovider": { + Router: &dynamic.Router{ + EntryPoints: []string{"web"}, + Service: "foo-service@myprovider", + Rule: "Host(`foo.baz`)", + Middlewares: []string{"auth", "addPrefixTest@anotherprovider"}, + TLS: &dynamic.RouterTLSConfig{}, + }, + Status: "enabled", + }, + }, + }, + expected: expected{ + statusCode: http.StatusOK, + jsonFile: "testdata/router-baz-default-tls-options.json", + }, + }, + { + desc: "one router by id, using specific TLS options", + path: "/api/http/routers/baz@myprovider", + conf: runtime.Configuration{ + Routers: map[string]*runtime.RouterInfo{ + "baz@myprovider": { + Router: &dynamic.Router{ + EntryPoints: []string{"web"}, + Service: "foo-service@myprovider", + Rule: "Host(`foo.baz`)", + Middlewares: []string{"auth", "addPrefixTest@anotherprovider"}, + TLS: &dynamic.RouterTLSConfig{ + Options: "myTLS", + }, + }, + Status: "enabled", + }, + }, + }, + expected: expected{ + statusCode: http.StatusOK, + jsonFile: "testdata/router-baz-custom-tls-options.json", + }, + }, { desc: "one router by id, that does not exist", path: "/api/http/routers/foo@myprovider", @@ -811,6 +857,7 @@ func TestHandler_HTTP(t *testing.T) { // To lazily initialize the Statuses. rtConf.PopulateUsedBy() rtConf.GetRoutersByEntryPoints(context.Background(), []string{"web"}, false) + rtConf.GetRoutersByEntryPoints(context.Background(), []string{"web"}, true) handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf) server := httptest.NewServer(handler.createRouter()) diff --git a/pkg/api/testdata/router-baz-custom-tls-options.json b/pkg/api/testdata/router-baz-custom-tls-options.json new file mode 100644 index 000000000..c95dc7949 --- /dev/null +++ b/pkg/api/testdata/router-baz-custom-tls-options.json @@ -0,0 +1,20 @@ +{ + "entryPoints": [ + "web" + ], + "middlewares": [ + "auth", + "addPrefixTest@anotherprovider" + ], + "name": "baz@myprovider", + "provider": "myprovider", + "rule": "Host(`foo.baz`)", + "service": "foo-service@myprovider", + "tls": { + "options": "myTLS" + }, + "status": "enabled", + "using": [ + "web" + ] +} diff --git a/pkg/api/testdata/router-baz-default-tls-options.json b/pkg/api/testdata/router-baz-default-tls-options.json new file mode 100644 index 000000000..054198047 --- /dev/null +++ b/pkg/api/testdata/router-baz-default-tls-options.json @@ -0,0 +1,20 @@ +{ + "entryPoints": [ + "web" + ], + "middlewares": [ + "auth", + "addPrefixTest@anotherprovider" + ], + "name": "baz@myprovider", + "provider": "myprovider", + "rule": "Host(`foo.baz`)", + "service": "foo-service@myprovider", + "tls": { + "options": "default" + }, + "status": "enabled", + "using": [ + "web" + ] +}