aboutsummaryrefslogtreecommitdiff
path: root/pim_ctrl.go
blob: 8589536cc2f299b2f42f7527eeb78908dd2276a0 (plain) (blame)
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
package main

import (
	"errors"
	"fmt"
	"os/exec"
	"strings"
	"slices"
	garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
	"github.com/go-ldap/ldap/v3"
)

const (
	FIELD_AEROGRAMME_CRYPTOROOT = "aero_cryptoroot"
	FIELD_AEROGRAMME_BUCKET_ID = "aero_bucket_id"
	FIELD_AEROGRAMME_BUCKET_NAME = "aero_bucket"
	LOCAL_ALIAS_NAME = "aerogramme"
)


var (
	ErrPimBuilderDirty              = fmt.Errorf("builder is dirty.")
	ErrPimBucketLocalAliasNotFound  = fmt.Errorf("local alias does not exist in garage or points to the wrong bucket.")
	ErrPimBucketIdEmpty             = fmt.Errorf("missing bucket ID in LDAP.")
	ErrPimBucketNameEmpty		= fmt.Errorf("missing bucket local garage alias in LDAP.")
	ErrPimBucketInfoNotFetched	= fmt.Errorf("bucket info has not been fetched.")
	ErrPimCryptoRootEmpty	 	= fmt.Errorf("missing cryptoroot in LDAP.")
	ErrPimCantCreateBucket	 	= fmt.Errorf("unable to create PIM bucket.")
)

type PimBuilder struct {
	user *LoggedUser
	cryptoroot string
	bucketId string
	bucketName string
	bucketInfo *garage.BucketInfo
	dirty bool
	errors []error
}

func NewPimBuilder(user *LoggedUser) *PimBuilder {
	return &PimBuilder {
		user: user,
		cryptoroot: user.Entry.GetAttributeValue(FIELD_AEROGRAMME_CRYPTOROOT),
		bucketId: user.Entry.GetAttributeValue(FIELD_AEROGRAMME_BUCKET_ID),
		bucketName: user.Entry.GetAttributeValue(FIELD_AEROGRAMME_BUCKET_NAME),
		bucketInfo: nil,
		dirty: false,
		errors: make([]error, 0),
	}
}
func (pm *PimBuilder) CheckCryptoRoot() *PimBuilder {
	if pm.cryptoroot == "" {
		cmd := exec.Command("./aerogramme", "tools", "crypto-root", "new-clear-text")
		var out strings.Builder
		cmd.Stdout = &out
		err := cmd.Run()
		if err != nil {
			pm.errors = append(pm.errors, err)
			return pm
		}
		pm.cryptoroot = out.String()
		pm.dirty = true
	}
	return pm
}

func (pm *PimBuilder) CheckBucket() *PimBuilder {
	keyInfo, err := pm.user.S3KeyInfo()
	if err != nil {
		pm.errors = append(pm.errors, err)
		return pm
	}

	if pm.bucketId == "" {
		candidateName := LOCAL_ALIAS_NAME
		var bInfo *garage.BucketInfo
		var err error

		err = nil
		for _, ext := range []string{"", "-1", "-2", "-3", "-4", "-5"} {
			candidateName = LOCAL_ALIAS_NAME + ext
			bInfo, err = grgCreateLocalBucket(candidateName, *keyInfo.AccessKeyId)
			if err == nil {
				break
			}
		}

		if err != nil {
			pm.errors = append(pm.errors, ErrPimCantCreateBucket)
			return pm
		}

		qr := pm.user.Quota.DefaultPimQuota()
		ur := garage.NewUpdateBucketRequest()
		ur.SetQuotas(*qr)	
		bInfo, err = grgUpdateBucket(*bInfo.Id, ur)
		if err != nil {
			pm.errors = append(pm.errors, err)
			return pm
		}

		pm.bucketId = *bInfo.Id
		pm.bucketName = candidateName
		pm.bucketInfo = bInfo
		pm.dirty = true
	} else {
		binfo, err := grgGetBucket(pm.bucketId)
		if err != nil {
			pm.errors = append(pm.errors, err)
			return pm
		}
		pm.bucketInfo = binfo

		//@TODO find my key, check that pm.bucketName exists in bucketLocalAliases
		nameFound := false
		for _, k := range binfo.Keys {
			if *k.AccessKeyId != *keyInfo.AccessKeyId {
				// not my key
				continue
			}
			if slices.Contains(k.BucketLocalAliases, pm.bucketName) {
				nameFound = true
				break
			}
		}
		if !nameFound {
			pm.errors = append(pm.errors, ErrPimBucketLocalAliasNotFound)
			return pm
		}
	}

	return pm
}

func (pm *PimBuilder) LdapUpdate() *PimBuilder {
	if len(pm.errors) > 0 {
		return pm
	}

	modify_request := ldap.NewModifyRequest(pm.user.Login.Info.DN(), nil)
	modify_request.Replace(FIELD_AEROGRAMME_CRYPTOROOT, []string{pm.cryptoroot})
	modify_request.Replace(FIELD_AEROGRAMME_BUCKET_NAME, []string{pm.bucketName})
	modify_request.Replace(FIELD_AEROGRAMME_BUCKET_ID, []string{pm.bucketId})
	err := pm.user.Login.conn.Modify(modify_request)
	if err != nil {
		pm.errors = append(pm.errors, err)
		return pm
	}

	pm.dirty = false
	return pm
}

func (pm *PimBuilder) Build() (*PimController, error) {
	// checks
	if pm.dirty {
		pm.errors = append(pm.errors, ErrPimBuilderDirty)
	}
	if pm.bucketId == "" {
		pm.errors = append(pm.errors, ErrPimBucketIdEmpty)
	}
	if pm.bucketName == "" {
		pm.errors = append(pm.errors, ErrPimBucketNameEmpty)
	}
	if pm.bucketInfo == nil {
		pm.errors = append(pm.errors, ErrPimBucketInfoNotFetched)
	}
	if pm.cryptoroot == "" {
		pm.errors = append(pm.errors, ErrPimCryptoRootEmpty)
	}
	if len(pm.errors) > 0 {
		err := errors.New("PIM Builder failed")
		for _, iterErr := range pm.errors {
			err = errors.Join(err, iterErr)
		}
		return nil, err
	}

	// quotas
	q := pm.bucketInfo.GetQuotas()
	size := NewQuotaStat(*pm.bucketInfo.Bytes, (&q).GetMaxSize(), true)
	objects := NewQuotaStat(*pm.bucketInfo.Objects, (&q).GetMaxObjects(), false)

	// final object
	pim_ctl := &PimController {
		BucketId: pm.bucketId,
		BucketName: pm.bucketName,
		Size: size,
		Files: objects,
		user: pm.user,
		bucketInfo: pm.bucketInfo,
		cryptoroot: pm.cryptoroot,

	}

	return pim_ctl, nil
}

// --- Controller ---
type PimController struct {
	BucketId    string `json:"bucket_id"`
	BucketName  string `json:"bucket_name"`
	Size	    QuotaStat `json:"quota_size"`
	Files	    QuotaStat `json:"quota_files"`
	user        *LoggedUser
	bucketInfo  *garage.BucketInfo
	cryptoroot  string
}

//@FIXME Implement quota bursting