-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
handler_helper.go
156 lines (131 loc) · 4.06 KB
/
handler_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package cli
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/ory/x/httpx"
"github.com/olekukonko/tablewriter"
"github.com/sawadashota/encrypta"
"github.com/spf13/cobra"
httptransport "github.com/go-openapi/runtime/client"
hydra "github.com/ory/hydra/sdk/go/hydra/client"
"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
)
func configureClient(cmd *cobra.Command) *hydra.OryHydra {
return configureClientBase(cmd, true)
}
type transport struct {
Transport http.RoundTripper
cmd *cobra.Command
}
func newTransport(cmd *cobra.Command) *transport {
return &transport{
cmd: cmd,
Transport: httpx.NewResilientRoundTripper(
&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: flagx.MustGetBool(cmd, "skip-tls-verify")},
},
time.Second,
flagx.MustGetDuration(cmd, "fail-after"),
).WithShouldRetry(
func(res *http.Response, err error) bool {
if err != nil {
fmt.Printf("Unable to connect: %s\n", err)
return true
} else if res.StatusCode == 0 || res.StatusCode >= 500 {
fmt.Printf(`Unable to connect to "%s", unexpected HTTP error status code: %d\n`, res.Request.URL.String(), res.StatusCode)
return true
}
return false
},
),
}
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
if flagx.MustGetBool(t.cmd, "fake-tls-termination") {
req.Header.Set("X-Forwarded-Proto", "https")
}
return t.Transport.RoundTrip(req)
}
func configureClientBase(cmd *cobra.Command, withAuth bool) *hydra.OryHydra {
u := RemoteURI(cmd)
ht := httptransport.New(
u.Host,
u.Path,
[]string{u.Scheme},
)
ht.Transport = newTransport(cmd)
if withAuth {
if token := flagx.MustGetString(cmd, "access-token"); token != "" {
ht.DefaultAuthentication = httptransport.BearerToken(token)
}
}
return hydra.New(ht, nil)
}
func configureClientWithoutAuth(cmd *cobra.Command) *hydra.OryHydra {
return configureClientBase(cmd, false)
}
func formatResponse(response interface{}) string {
out, err := json.MarshalIndent(response, "", "\t")
cmdx.Must(err, `Command failed because an error ("%s") occurred while prettifying output`, err)
return string(out)
}
// newTable is table renderer at console
// And defines table layout option
//
// https://github.com/olekukonko/tablewriter
func newTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
// render options
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
return table
}
// newEncryptionKey for client secret
func newEncryptionKey(cmd *cobra.Command, client *http.Client) (ek encrypta.EncryptionKey, encryptSecret bool, err error) {
if client == nil {
client = http.DefaultClient
}
pgpKey := flagx.MustGetString(cmd, "pgp-key")
pgpKeyURL := flagx.MustGetString(cmd, "pgp-key-url")
keybaseUsername := flagx.MustGetString(cmd, "keybase")
if pgpKey != "" {
ek, err = encrypta.NewPublicKeyFromBase64Encoded(pgpKey)
encryptSecret = true
return
}
if pgpKeyURL != "" {
ek, err = encrypta.NewPublicKeyFromURL(pgpKeyURL, encrypta.HTTPClientOption(client))
encryptSecret = true
return
}
if keybaseUsername != "" {
ek, err = encrypta.NewPublicKeyFromKeybase(keybaseUsername, encrypta.HTTPClientOption(client))
encryptSecret = true
return
}
return nil, false, nil
}