-
Notifications
You must be signed in to change notification settings - Fork 3
/
urlbuilder.go
162 lines (148 loc) · 4.81 KB
/
urlbuilder.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
157
158
159
160
161
162
// Copyright (C) 2021 CGI France
//
// This file is part of LINO.
//
// LINO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LINO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LINO. If not, see <http://www.gnu.org/licenses/>.
package urlbuilder
import (
"fmt"
"io"
"net/url"
"os"
"strings"
"github.com/cgi-fr/lino/internal/app/localstorage"
"github.com/cgi-fr/lino/pkg/dataconnector"
"github.com/docker/docker-credential-helpers/client"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/xo/dburl"
)
// GenOracleRaw generates a Go Driver for Oracle (godror) DSN from the passed URL.
func GenOracleRaw(u *dburl.URL) (string, error) {
// example of DSN generated :
// `user="login" password="password" connectString="host:port/service_name" sysdba=true`
connectionString := strings.TrimSuffix(u.Host+u.Path, "/")
dsn := fmt.Sprintf("connectString=\"%s\"", connectionString)
for value := range u.Query() {
dsn += fmt.Sprintf(" %s=\"%s\"", value, u.Query().Get(value))
}
// build user/pass
if u.User != nil {
if un := u.User.Username(); len(un) > 0 {
dsn += fmt.Sprintf(" user=\"%s\"", un)
if up, ok := u.User.Password(); ok {
dsn += fmt.Sprintf(" password=\"%s\"", up)
}
}
}
return dsn, nil
}
func init() {
oracleScheme := dburl.Scheme{
Driver: "godror-raw",
Generator: GenOracleRaw,
Transport: dburl.TransportAny,
Opaque: false,
Aliases: []string{"oracle-raw"},
Override: "godror",
}
dburl.Register(oracleScheme)
db2Scheme := dburl.Scheme{
Driver: "db2",
Generator: func(u *dburl.URL) (string, error) {
password, _ := u.User.Password()
database := strings.TrimPrefix(u.Path, "/")
result := fmt.Sprintf("HOSTNAME=%s;DATABASE=%s;PORT=%s;UID=%s;PWD=%s", u.Hostname(), database, u.Port(), u.User.Username(), password)
return result, nil
},
Transport: dburl.TransportAny,
Opaque: false,
Aliases: []string{"go_ibm_db"},
Override: "go_ibm_db",
}
dburl.Register(db2Scheme)
httpScheme := dburl.Scheme{
Driver: "http",
Generator: dburl.GenFromURL("http://localhost:8080"),
Transport: dburl.TransportAny,
Opaque: false,
Aliases: []string{"https"},
Override: "",
}
dburl.Register(httpScheme)
}
func BuildURL(dc *dataconnector.DataConnector, out io.Writer) *dburl.URL {
u, e2 := dburl.Parse(dc.URL)
if e2 != nil {
fmt.Fprintln(out, e2.Error())
os.Exit(3)
}
// get user from env
if dc.User.ValueFromEnv != "" {
userFromEnv := os.Getenv(dc.User.ValueFromEnv)
if userFromEnv == "" {
if out != nil {
fmt.Fprintf(out, "warn: missing environment variable %s", dc.User.ValueFromEnv)
fmt.Fprintln(out)
}
} else {
u.User = url.User(userFromEnv)
}
} else if dc.User.Value != "" {
// set user from dc
u.User = url.User(dc.User.Value)
}
// get password from env
if dc.Password.ValueFromEnv != "" {
passwordFromEnv := os.Getenv(dc.Password.ValueFromEnv)
if passwordFromEnv == "" {
if out != nil {
fmt.Fprintf(out, "warn: missing environment variable %s", dc.Password.ValueFromEnv)
fmt.Fprintln(out)
}
} else {
u.User = url.UserPassword(u.User.Username(), passwordFromEnv)
}
}
// if credentials still missing, check default store
username := u.User.Username()
_, passwordIsSet := u.User.Password()
if username == "" || !passwordIsSet {
store := defaultCredentialsStore()
creds, err := client.Get(store, u.String())
if err != nil {
// failed to use credential store backend, fallback to local storage
creds, err = localstorage.Read(u.String())
}
if err == nil {
u.User = url.UserPassword(creds.Username, creds.Secret)
}
}
return u
}
func StorePassword(u *dburl.URL, password string, out io.Writer) error {
store := defaultCredentialsStore()
creds := &credentials.Credentials{ServerURL: u.URL.String(), Username: u.URL.User.Username(), Secret: password}
err := client.Store(store, creds)
if err != nil {
// failed to use credential store backend
if credentials.IsCredentialsMissingServerURL(err) || credentials.IsCredentialsMissingUsername(err) || credentials.IsErrCredentialsNotFound(err) {
return err
}
// fall back to local storage
fmt.Fprintf(out, "warn: password will be stored unencrypted in %s, configure a credential helper to remove this warning. See https://github.com/docker/docker-credential-helpers", localstorage.GetFileLocation())
fmt.Fprintln(out)
return localstorage.Store(creds)
}
return nil
}