Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support to ory hydra running in secure mode #62

Merged
merged 29 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6491a99
Support to ory hydra running in secure mode
fjvierap Dec 18, 2020
d769f7d
feat: adjust readme
fjvierap Dec 18, 2020
daf5c44
Add namespace bases roles instead of cluster
fjvierap Feb 11, 2021
7d66a70
Add log message when enable insecure skip verify
fjvierap Apr 23, 2021
d90f5a9
Add setup log for insecure verify
fjvierap Apr 23, 2021
509ee6a
Adjust README.md
fjvierap Apr 23, 2021
90f8234
Merge remote-tracking branch 'origin/master' into namespace
fjvierap Apr 23, 2021
715ee4a
Add error for not existent tls trust store
fjvierap Apr 28, 2021
51a4e30
Merge pull request #1 from fjvierap/namespace
fjvierap Apr 28, 2021
e16c267
Adjust rbac
fjvierap Apr 28, 2021
b75d417
Add unit test for create http client
fjvierap May 5, 2021
5364f44
Remove HydraClientMaker
fjvierap May 5, 2021
d403834
Improve error handling
fjvierap May 10, 2021
d12ad0a
Add helpers to makefile for testing that package
fjvierap May 10, 2021
9b8f463
build: update CRDs and k8s dependencies (#68)
colunira May 10, 2021
9d56503
docs: Incorporates changes from version v0.0.20
May 10, 2021
14611a8
Support to ory hydra running in secure mode
fjvierap Dec 18, 2020
7f2bf13
feat: adjust readme
fjvierap Dec 18, 2020
cde714e
Add namespace bases roles instead of cluster
fjvierap Feb 11, 2021
bbd2830
Add log message when enable insecure skip verify
fjvierap Apr 23, 2021
a210259
Add setup log for insecure verify
fjvierap Apr 23, 2021
00d3a80
Adjust README.md
fjvierap Apr 23, 2021
1ff476c
Add error for not existent tls trust store
fjvierap Apr 28, 2021
b8622e0
Adjust rbac
fjvierap Apr 28, 2021
ba61fce
Add unit test for create http client
fjvierap May 5, 2021
9c5a153
Remove HydraClientMaker
fjvierap May 5, 2021
27558c3
Improve error handling
fjvierap May 10, 2021
aaf8870
Add helpers to makefile for testing that package
fjvierap May 10, 2021
80230cd
Merge branch 'master' of https://github.com/fjvierap/hydra-maester
fjvierap May 10, 2021
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
11 changes: 8 additions & 3 deletions helpers/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package helpers
import (
"crypto/tls"
"net/http"
"os"

ctrl "sigs.k8s.io/controller-runtime"

httptransport "github.com/go-openapi/runtime/client"
)

func CreateHttpClient(insecureSkipVerify bool, tlsTrustStore string) *http.Client {
func CreateHttpClient(insecureSkipVerify bool, tlsTrustStore string) (*http.Client, error) {
setupLog := ctrl.Log.WithName("setup")
tr := &http.Transport{}
httpClient := &http.Client{}
Expand All @@ -19,15 +20,19 @@ func CreateHttpClient(insecureSkipVerify bool, tlsTrustStore string) *http.Clien
httpClient.Transport = tr
}
if tlsTrustStore != "" {
if _, err := os.Stat(tlsTrustStore); err != nil {
return nil, err
}

setupLog.Info("configuring TLS with tlsTrustStore")
ops := httptransport.TLSClientOptions{
CA: tlsTrustStore,
InsecureSkipVerify: insecureSkipVerify,
}
if tlsClient, err := httptransport.TLSClient(ops); err != nil {
setupLog.Error(err, "Error while getting TLSClient, default http client will be used")
return tlsClient
return tlsClient, nil
}
}
return httpClient
return httpClient, nil
}
22 changes: 17 additions & 5 deletions helpers/http_client_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
package helpers
package helpers_test

import (
"io/ioutil"
"os"
"testing"

"github.com/ory/hydra-maester/helpers"

"github.com/stretchr/testify/require"
)

func TestCreateHttpClient(t *testing.T) {
t.Run("should create insecureSkipVerify client", func(t *testing.T) {
client := CreateHttpClient(true, "")
client, err := helpers.CreateHttpClient(true, "")
require.NotNil(t, client)
require.Nil(t, err)
})

t.Run("should create client with insecureSkipVerify and wrong tlsTrustStore", func(t *testing.T) {
tlsTrustStore := "some path"
client := CreateHttpClient(true, tlsTrustStore)
client, err := helpers.CreateHttpClient(true, tlsTrustStore)
require.Nil(t, client)
require.Nil(t, err)
})

t.Run("should create client with and tlsTrustStore", func(t *testing.T) {
file, err := ioutil.TempFile("/tmp", "test")
require.Nil(t, err)
client := CreateHttpClient(true, file.Name())
client, err := helpers.CreateHttpClient(true, file.Name())
defer os.Remove(file.Name())
require.NotNil(t, client)
require.Nil(t, err)
})

t.Run("should not create client with and wrong tlsTrustStore", func(t *testing.T) {
client, err := helpers.CreateHttpClient(true, "/somefile")
require.NotNil(t, client)
require.NotNil(t, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we control what error will be returned here, maybe we could check not only if the err is not nil, but if it the one we expect?

})

t.Run("should create client without and tlsTrustStore", func(t *testing.T) {
client := CreateHttpClient(true, "")
client, err := helpers.CreateHttpClient(true, "")
require.NotNil(t, client)
require.Nil(t, err)
})
}
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func main() {
}
}

hydraClient := getHydraClient(defaultSpec, tlsTrustStore, insecureSkipVerify)
hydraClient, err := getHydraClient(defaultSpec, tlsTrustStore, insecureSkipVerify)
if err != nil {
setupLog.Error(err, "making default hydra client", "controller", "OAuth2Client")
os.Exit(1)
Expand All @@ -130,22 +130,27 @@ func main() {
}
}

func getHydraClient(spec hydrav1alpha1.OAuth2ClientSpec, tlsTrustStore string, insecureSkipVerify bool) controllers.HydraClientInterface {
func getHydraClient(spec hydrav1alpha1.OAuth2ClientSpec, tlsTrustStore string, insecureSkipVerify bool) (controllers.HydraClientInterface, error) {

address := fmt.Sprintf("%s:%d", spec.HydraAdmin.URL, spec.HydraAdmin.Port)
u, err := url.Parse(address)
if err != nil {
return nil
return nil, err
}

c, err := helpers.CreateHttpClient(insecureSkipVerify, tlsTrustStore)
if err != nil {
return nil, err
}

client := &hydra.Client{
HydraURL: *u.ResolveReference(&url.URL{Path: spec.HydraAdmin.Endpoint}),
HTTPClient: helpers.CreateHttpClient(insecureSkipVerify, tlsTrustStore),
HTTPClient: c,
}

if spec.HydraAdmin.ForwardedProto != "" && spec.HydraAdmin.ForwardedProto != "off" {
client.ForwardedProto = spec.HydraAdmin.ForwardedProto
}

return client
return client, nil
}