Skip to content

Commit 518854b

Browse files
committed
Moderately complete refactor on everything but init
1 parent bb79ae7 commit 518854b

7 files changed

Lines changed: 50 additions & 77 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
)
3535

3636
require (
37-
github.com/NHAS/tetcd v0.0.32-beta // indirect
37+
github.com/NHAS/tetcd v0.0.33-beta // indirect
3838
github.com/beorn7/perks v1.0.1 // indirect
3939
github.com/boombuler/barcode v1.1.0 // indirect
4040
github.com/caddyserver/zerossl v0.1.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ github.com/NHAS/tetcd v0.0.31-beta h1:iDcyZVewo+4BrxT9Bb1NxAhf1eLpcN73iO185wCssL
5454
github.com/NHAS/tetcd v0.0.31-beta/go.mod h1:YskRbp5UvrsA8XDF3yTsw8t1GctEAP+pKNKHjceVO3k=
5555
github.com/NHAS/tetcd v0.0.32-beta h1:7CpcJvFsuXx6xaNfuaiVy0QQAzCSJQu/6UOwS1XhoK4=
5656
github.com/NHAS/tetcd v0.0.32-beta/go.mod h1:YskRbp5UvrsA8XDF3yTsw8t1GctEAP+pKNKHjceVO3k=
57+
github.com/NHAS/tetcd v0.0.33-beta h1:e9QDrXdWXs16BVDTCN93yp3OaHhYTnU1aHZUsLn+NYQ=
58+
github.com/NHAS/tetcd v0.0.33-beta/go.mod h1:YskRbp5UvrsA8XDF3yTsw8t1GctEAP+pKNKHjceVO3k=
5759
github.com/NHAS/webauthn v0.0.0-20240606085832-ea3172ef4dfa h1:z8Lo9+R9h4ZF5qvq2NTrWGVjL8gE92cPUv9J4i4yYKg=
5860
github.com/NHAS/webauthn v0.0.0-20240606085832-ea3172ef4dfa/go.mod h1:WfTnekCrJZ8MTDSlOeFACJTeGFvQnLInNzbZLRpoqTU=
5961
github.com/NHAS/wireguard-go v0.0.0-20250612003640-64654948f25c h1:85LIaUo0uEVO16vrRfuY+8SVLqYqdsANf3RPC7QNTyQ=

internal/data/acls.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ func (d *database) SetAcl(effects string, policy acls.Acl, overwrite bool) error
2727
return Config.Acls.Policies().Key(effects).Put(context.Background(), d.etcd, &policy)
2828
}
2929

30-
func (d *database) GetPolicies() (result []control.PolicyData, err error) {
30+
func (d *database) GetPolicies() (policies []control.PolicyData, err error) {
3131

32-
order, values, err := Config.Acls.Policies().List(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
32+
result, err := Config.Acls.Policies().List(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
3333
if err != nil {
3434
return nil, err
3535
}
3636

37-
for _, r := range order {
37+
for _, r := range result.Order {
3838

39-
result = append(result, control.PolicyData{
39+
policies = append(policies, control.PolicyData{
4040
Effects: r,
41-
PublicRoutes: values[r].Allow,
42-
MfaRoutes: values[r].Mfa,
43-
DenyRoutes: values[r].Deny,
41+
PublicRoutes: result.Values[r].Allow,
42+
MfaRoutes: result.Values[r].Mfa,
43+
DenyRoutes: result.Values[r].Deny,
4444
})
4545
}
4646

internal/data/devices.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,7 @@ func (d *database) AuthoriseDevice(username, address string) error {
167167
}
168168

169169
func (d *database) GetAllSessions() (sessions []config.DeviceSession, err error) {
170-
171-
order, data, err := InternalConfig.Devices.Sessions().List(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
172-
if err != nil {
173-
return nil, err
174-
}
175-
176-
// otherwise json returns null
177-
sessions = []config.DeviceSession{}
178-
for _, session := range order {
179-
sessions = append(sessions, data[session])
180-
}
181-
182-
return sessions, nil
170+
return InternalConfig.Devices.Sessions().Entries(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
183171
}
184172

185173
func (d *database) markDeviceSessionStarted(address, username string) error {

internal/data/registration.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,7 @@ func (d *database) GetRegistrationToken(token string) (username, overwrites, sta
6868

6969
// Returns list of tokens
7070
func (d *database) GetRegistrationTokens() (results []control.RegistrationResult, err error) {
71-
72-
order, tokens, err := InternalConfig.RegistrationTokens().List(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
for _, token := range order {
78-
results = append(results, tokens[token])
79-
}
80-
81-
return results, nil
71+
return InternalConfig.RegistrationTokens().Entries(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
8272
}
8373

8474
func (d *database) DeleteRegistrationToken(identifier string) error {

internal/data/webhooks.go

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,67 +52,60 @@ func (d *database) GetWebhookLastRequest(id string) (string, error) {
5252

5353
func (d *database) GetWebhooks() (hooks []WebhookGetResponseDTO, err error) {
5454

55-
order, data, err := InternalConfig.Webhooks.Active().List(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
55+
result, err := InternalConfig.Webhooks.Active().List(context.Background(), d.etcd, clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
5656
if err != nil {
5757
return nil, err
5858
}
5959

6060
// otherwise json returns null
61-
hooks = make([]WebhookGetResponseDTO, 0, len(data))
61+
hooks = make([]WebhookGetResponseDTO, 0, len(result.Values))
6262

6363
txn := tetcd.NewTxn(context.Background(), d.etcd)
6464
then := txn.Then()
6565

66-
for _, id := range order {
67-
hooks = append(hooks, WebhookGetResponseDTO{Webhook: data[id]})
68-
69-
tetcd.GetTx(then, InternalConfig.Webhooks.LastRequests.Time().Key(id), clientv3.WithRev(response.Header.Revision))
70-
tetcd.GetTx(then, InternalConfig.Webhooks.LastRequests.Status().Key(id), clientv3.WithRev(response.Header.Revision))
66+
type lastRequestDataHandles struct {
67+
time *tetcd.GetHandle[time.Time]
68+
status *tetcd.GetHandle[string]
7169
}
7270

73-
resp, err := d.etcd.Txn(context.Background()).Then(lastRequestOps...).Commit()
74-
// we'll just have no last_request times so this isnt critical
75-
// intentional == nil
76-
if err == nil {
77-
for i := range resp.Responses {
71+
handles := make([]lastRequestDataHandles, 0, len(result.Order))
7872

79-
if len(resp.Responses[i].GetResponseRange().Kvs) == 0 {
80-
// webhook has never fired so ignore
81-
continue
82-
}
73+
for _, id := range result.Order {
74+
hooks = append(hooks, WebhookGetResponseDTO{Webhook: result.Values[id]})
8375

84-
var t time.Time
85-
err = json.Unmarshal(resp.Responses[i].GetResponseRange().Kvs[0].Value, &t)
86-
if err != nil {
87-
log.Error().Err(err).Str("last_request", string(resp.Responses[i].GetResponseRange().Kvs[0].Key)).Msg("could not unmarshal last request time from webhook")
88-
continue
89-
}
76+
handles = append(handles,
77+
lastRequestDataHandles{
78+
time: tetcd.GetTx(then,
79+
InternalConfig.Webhooks.LastRequests.Time().Key(id),
80+
clientv3.WithRev(result.Rev),
81+
),
9082

91-
// As we're generating the txn list in order of the hooks we can do this. Its bad code and Im sure it'll blow up, but hey. Funni
92-
hooks[i].LastRequestTime = t
93-
}
83+
status: tetcd.GetTx(then,
84+
InternalConfig.Webhooks.LastRequests.Status().Key(id),
85+
clientv3.WithRev(result.Rev),
86+
),
87+
})
9488
}
9589

96-
resp, err = d.etcd.Txn(context.Background()).Then(lastRequestStatusOps...).Commit()
97-
// we'll just have no last_request status' so this isnt critical
90+
err = txn.Commit()
91+
// we'll just have no last_request times so this isnt critical
9892
// intentional == nil
9993
if err == nil {
100-
for i := range resp.Responses {
94+
for i := range handles {
10195

102-
if len(resp.Responses[i].GetResponseRange().Kvs) == 0 {
103-
// webhook has never fired so ignore
96+
status, err := handles[i].status.Value()
97+
if err != nil {
98+
log.Info().Err(err).Msg("could not fetch last request status from webhook")
10499
continue
105100
}
106-
107-
var status string
108-
err = json.Unmarshal(resp.Responses[i].GetResponseRange().Kvs[0].Value, &status)
101+
time, err := handles[i].time.Value()
109102
if err != nil {
110-
111-
log.Info().Err(err).Str("last_request", string(resp.Responses[i].GetResponseRange().Kvs[0].Key)).Msg("could not unmarshal last request status from webhook")
103+
log.Info().Err(err).Msg("could not fetch last request time from webhook")
112104
continue
113105
}
114106

115107
// As we're generating the txn list in order of the hooks we can do this. Its bad code and Im sure it'll blow up, but hey. Funni
108+
hooks[i].LastRequestTime = time
116109
hooks[i].LastRequestStatus = status
117110
}
118111
}
@@ -313,22 +306,22 @@ func (d *database) CreateTempWebhook() (string, string, error) {
313306
return temp.ID, authHeader, err
314307
}
315308

316-
func (d *database) DeleteWebhooks(ids []string, txn *tetcd.TxnConditional) error {
309+
func (d *database) DeleteWebhooks(ids []string) error {
317310

318-
var ops []clientv3.Op
311+
txn := tetcd.NewTxn(context.Background(), d.etcd)
312+
then := txn.Then()
319313

320314
for _, id := range ids {
321315

322-
tetcd.DeleteTx(txn, InternalConfig.Webhooks.Active().Key(id), clientv3.WithPrefix())
323-
tetcd.DeleteTx(txn, InternalConfig.Webhooks.Auth().Key(id), clientv3.WithPrefix())
316+
tetcd.DeleteTx(then, InternalConfig.Webhooks.Active().Key(id), clientv3.WithPrefix())
317+
tetcd.DeleteTx(then, InternalConfig.Webhooks.Auth().Key(id), clientv3.WithPrefix())
324318
// this is a little gross, it might be better to make last requests a map
325-
tetcd.DeleteTx(txn, InternalConfig.Webhooks.LastRequests.Data().Key(id), clientv3.WithPrefix())
326-
tetcd.DeleteTx(txn, InternalConfig.Webhooks.LastRequests.Status().Key(id), clientv3.WithPrefix())
327-
tetcd.DeleteTx(txn, InternalConfig.Webhooks.LastRequests.Time().Key(id), clientv3.WithPrefix())
319+
tetcd.DeleteTx(then, InternalConfig.Webhooks.LastRequests.Data().Key(id), clientv3.WithPrefix())
320+
tetcd.DeleteTx(then, InternalConfig.Webhooks.LastRequests.Status().Key(id), clientv3.WithPrefix())
321+
tetcd.DeleteTx(then, InternalConfig.Webhooks.LastRequests.Time().Key(id), clientv3.WithPrefix())
328322
}
329323

330-
_, err := d.etcd.Txn(context.Background()).Then(ops...).Commit()
331-
return err
324+
return txn.Commit()
332325
}
333326

334327
func Unpack(parent string, c map[string]any) []WebhookAttribute {

test/integration/webhooks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func createValidWebhook() data.WebhookCreateRequestDTO {
1919
var newHook data.WebhookCreateRequestDTO
2020
newHook.ID, _ = utils.GenerateRandomHex(16)
21-
newHook.Action = string(config.CreateRegistrationToken)
21+
newHook.Action = config.CreateRegistrationToken
2222
newHook.AuthHeader, _ = utils.GenerateRandomHex(16)
2323
newHook.JsonAttributeRoles.AsUsername = "username"
2424
newHook.JsonAttributeRoles.AsRegistrationToken = "token"

0 commit comments

Comments
 (0)