forked from ccin2p3/go-freeipa
/
client.go
256 lines (221 loc) · 6.75 KB
/
client.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Package freeipa provides a client for the FreeIPA API.
It provides access to almost all methods available through the API.
Every API method has generated go structs for request parameters and output.
This code is generated from a schema which was queried from a FreeIPA
server using its "schema" method. This client performs basic response validation.
Since the FreeIPA server does not always conform to its own schema, it can
happen that this libary fails to unmarshal a response from FreeIPA.
If you run into that, please open an issue for this client library.
With that said, this is still the most extensive golang FreeIPA client
and it's probably easier to fix those issues here than to write
a new client from scratch.
Since FreeIPA cares about the presence or abscence of fields in requests,
all optional fields are defined as pointers. There are utility functions like
freeipa.String to make filling these less painful.
The client uses FreeIPA's JSON-RPC interface with username/password authentication.
There is no support for connecting to FreeIPA with Kerberos authentication.
There is currently no support for batched requests.
See https://github.com/tehwalris/go-freeipa/blob/master/developing.md
for information on how this library is generated.
*/
package freeipa
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
k5client "github.com/jcmturner/gokrb5/v8/client"
k5config "github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/pkg/errors"
)
// Client holds a connection to a FreeIPA server.
type Client struct {
host string
hc *http.Client
user string
pw string
k5client *k5client.Client
}
// Error is an error returned by the FreeIPA server in a JSON response.
type Error struct {
Message string `json:"message"`
Code int `json:"code"`
Name string `json:"name"`
}
func (t *Error) Error() string {
return fmt.Sprintf("%v (%v): %v", t.Name, t.Code, t.Message)
}
func NewClient(host string, tspt *http.Transport, user, pw string) (*Client, error) {
jar, e := cookiejar.New(&cookiejar.Options{
PublicSuffixList: nil, // this should be fine, since we only use one server
})
if e != nil {
return nil, e
}
c := &Client{
host: host,
hc: &http.Client{
Transport: tspt,
Jar: jar,
},
user: user,
pw: pw,
}
return c, nil
}
// Connect connects to the FreeIPA server and performs an initial login.
func Connect(host string, tspt *http.Transport, user, pw string) (*Client, error) {
jar, e := cookiejar.New(&cookiejar.Options{
PublicSuffixList: nil, // this should be fine, since we only use one server
})
if e != nil {
return nil, e
}
c := &Client{
host: host,
hc: &http.Client{
Transport: tspt,
Jar: jar,
},
user: user,
pw: pw,
}
if e := c.login(); e != nil {
return nil, errors.WithMessage(e, "initial login failed")
}
return c, nil
}
func ConnectWithKerberos(host string, tspt *http.Transport, k5ConnectOpts *KerberosConnectOptions) (*Client, error) {
jar, e := cookiejar.New(&cookiejar.Options{
PublicSuffixList: nil, // this should be fine, since we only use one server
})
if e != nil {
return nil, e
}
krb5Config, err := k5config.NewFromReader(k5ConnectOpts.Krb5ConfigReader)
if err != nil {
return nil, errors.WithMessage(err, "reading kerberos configuration")
}
ktBytes, err := ioutil.ReadAll(k5ConnectOpts.KeytabReader)
if err != nil {
return nil, errors.WithMessage(err, "reading keytab")
}
kt := keytab.New()
if err := kt.Unmarshal(ktBytes); err != nil {
return nil, errors.WithMessage(err, "parsing keytab")
}
k5client := k5client.NewWithKeytab(k5ConnectOpts.Username, k5ConnectOpts.Realm, kt, krb5Config)
c := &Client{
host: host,
hc: &http.Client{
Transport: tspt,
Jar: jar,
},
user: k5ConnectOpts.Username,
k5client: k5client,
}
if e := c.login(); e != nil {
return nil, fmt.Errorf("initial login falied: %v", e)
}
return c, nil
}
func (c *Client) ChangePassword(password string) error {
data := url.Values{
"user": []string{c.user},
"old_password": []string{c.pw},
"new_password": []string{password},
}
res, e := c.hc.PostForm(fmt.Sprintf("https://%v/ipa/session/change_password", c.host), data)
if e != nil {
return e
}
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusUnauthorized {
fmt.Println(res)
return unauthorizedHTTPResponseToFreeipaError(res)
}
return fmt.Errorf("unexpected http status code: %v", res.StatusCode)
}
fmt.Println(res.Header.Get("X-Ipa-Pwchange-Policy-Error"))
fmt.Println(res.Header.Get("X-Ipa-Pwchange-Result"))
return nil
}
func (c *Client) exec(req *request) (io.ReadCloser, error) {
res, e := c.sendRequest(req)
if e != nil {
return nil, e
}
if res.StatusCode == http.StatusUnauthorized {
res.Body.Close()
if e := c.login(); e != nil {
return nil, errors.WithMessage(e, "renewed login failed")
}
res, e = c.sendRequest(req)
if e != nil {
return nil, e
}
}
if res.StatusCode != http.StatusOK {
res.Body.Close()
return nil, fmt.Errorf("unexpected http status code: %v", res.StatusCode)
}
return res.Body, nil
}
func (c *Client) login() error {
if c.k5client != nil {
return c.loginWithKerberos()
}
data := url.Values{
"user": []string{c.user},
"password": []string{c.pw},
}
res, e := c.hc.PostForm(fmt.Sprintf("https://%v/ipa/session/login_password", c.host), data)
if e != nil {
return e
}
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusUnauthorized {
return unauthorizedHTTPResponseToFreeipaError(res)
}
return fmt.Errorf("unexpected http status code: %v", res.StatusCode)
}
return nil
}
func (c *Client) loginWithKerberos() error {
k5LoginEndpoint := fmt.Sprintf("https://%s/ipa/session/login_kerberos", c.host)
spnegoCl := spnego.NewClient(c.k5client, c.hc, "")
req, err := http.NewRequest("POST", k5LoginEndpoint, nil)
if err != nil {
return errors.WithMessage(err, "building login HTTP request")
}
req.Header.Add("Referer", fmt.Sprintf("https://%s/ipa", c.host))
res, err := spnegoCl.Do(req)
if err != nil {
return errors.Wrap(err, "logging in using Kerberos")
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected http status code: %v", res.StatusCode)
}
return nil
}
func (c *Client) sendRequest(req *request) (*http.Response, error) {
reqB, e := json.Marshal(req)
if e != nil {
return nil, e
}
reqH, e := http.NewRequest("POST", fmt.Sprintf("https://%v/ipa/session/json", c.host), bytes.NewBuffer(reqB))
if e != nil {
return nil, e
}
reqH.Header.Set("Content-Type", "application/json")
reqH.Header.Set("Accept", "application/json")
reqH.Header.Set("Referer", fmt.Sprintf("https://%v/ipa/ui", c.host))
return c.hc.Do(reqH)
}