aboutsummaryrefslogtreecommitdiff
path: root/weed/credential/migration.go
blob: b286bce62e33e46991e4d6a1677517aa9bf02f35 (plain)
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
package credential

import (
	"context"
	"fmt"

	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
	"github.com/seaweedfs/seaweedfs/weed/util"
)

// MigrateCredentials migrates credentials from one store to another
func MigrateCredentials(fromStoreName, toStoreName CredentialStoreTypeName, configuration util.Configuration, fromPrefix, toPrefix string) error {
	ctx := context.Background()

	// Create source credential manager
	fromCM, err := NewCredentialManager(fromStoreName, configuration, fromPrefix)
	if err != nil {
		return fmt.Errorf("failed to create source credential manager (%s): %v", fromStoreName, err)
	}
	defer fromCM.Shutdown()

	// Create destination credential manager
	toCM, err := NewCredentialManager(toStoreName, configuration, toPrefix)
	if err != nil {
		return fmt.Errorf("failed to create destination credential manager (%s): %v", toStoreName, err)
	}
	defer toCM.Shutdown()

	// Load configuration from source
	glog.Infof("Loading configuration from %s store...", fromStoreName)
	config, err := fromCM.LoadConfiguration(ctx)
	if err != nil {
		return fmt.Errorf("failed to load configuration from source store: %v", err)
	}

	if config == nil || len(config.Identities) == 0 {
		glog.Info("No identities found in source store")
		return nil
	}

	glog.Infof("Found %d identities in source store", len(config.Identities))

	// Migrate each identity
	var migrated, failed int
	for _, identity := range config.Identities {
		glog.V(1).Infof("Migrating user: %s", identity.Name)

		// Check if user already exists in destination
		existingUser, err := toCM.GetUser(ctx, identity.Name)
		if err != nil && err != ErrUserNotFound {
			glog.Errorf("Failed to check if user %s exists in destination: %v", identity.Name, err)
			failed++
			continue
		}

		if existingUser != nil {
			glog.Warningf("User %s already exists in destination store, skipping", identity.Name)
			continue
		}

		// Create user in destination
		err = toCM.CreateUser(ctx, identity)
		if err != nil {
			glog.Errorf("Failed to create user %s in destination store: %v", identity.Name, err)
			failed++
			continue
		}

		migrated++
		glog.V(1).Infof("Successfully migrated user: %s", identity.Name)
	}

	glog.Infof("Migration completed: %d migrated, %d failed", migrated, failed)

	if failed > 0 {
		return fmt.Errorf("migration completed with %d failures", failed)
	}

	return nil
}

// ExportCredentials exports credentials from a store to a configuration
func ExportCredentials(storeName CredentialStoreTypeName, configuration util.Configuration, prefix string) (*iam_pb.S3ApiConfiguration, error) {
	ctx := context.Background()

	// Create credential manager
	cm, err := NewCredentialManager(storeName, configuration, prefix)
	if err != nil {
		return nil, fmt.Errorf("failed to create credential manager (%s): %v", storeName, err)
	}
	defer cm.Shutdown()

	// Load configuration
	config, err := cm.LoadConfiguration(ctx)
	if err != nil {
		return nil, fmt.Errorf("failed to load configuration: %v", err)
	}

	return config, nil
}

// ImportCredentials imports credentials from a configuration to a store
func ImportCredentials(storeName CredentialStoreTypeName, configuration util.Configuration, prefix string, config *iam_pb.S3ApiConfiguration) error {
	ctx := context.Background()

	// Create credential manager
	cm, err := NewCredentialManager(storeName, configuration, prefix)
	if err != nil {
		return fmt.Errorf("failed to create credential manager (%s): %v", storeName, err)
	}
	defer cm.Shutdown()

	// Import each identity
	var imported, failed int
	for _, identity := range config.Identities {
		glog.V(1).Infof("Importing user: %s", identity.Name)

		// Check if user already exists
		existingUser, err := cm.GetUser(ctx, identity.Name)
		if err != nil && err != ErrUserNotFound {
			glog.Errorf("Failed to check if user %s exists: %v", identity.Name, err)
			failed++
			continue
		}

		if existingUser != nil {
			glog.Warningf("User %s already exists, skipping", identity.Name)
			continue
		}

		// Create user
		err = cm.CreateUser(ctx, identity)
		if err != nil {
			glog.Errorf("Failed to create user %s: %v", identity.Name, err)
			failed++
			continue
		}

		imported++
		glog.V(1).Infof("Successfully imported user: %s", identity.Name)
	}

	glog.Infof("Import completed: %d imported, %d failed", imported, failed)

	if failed > 0 {
		return fmt.Errorf("import completed with %d failures", failed)
	}

	return nil
}

// ValidateCredentials validates that all credentials in a store are accessible
func ValidateCredentials(storeName CredentialStoreTypeName, configuration util.Configuration, prefix string) error {
	ctx := context.Background()

	// Create credential manager
	cm, err := NewCredentialManager(storeName, configuration, prefix)
	if err != nil {
		return fmt.Errorf("failed to create credential manager (%s): %v", storeName, err)
	}
	defer cm.Shutdown()

	// Load configuration
	config, err := cm.LoadConfiguration(ctx)
	if err != nil {
		return fmt.Errorf("failed to load configuration: %v", err)
	}

	if config == nil || len(config.Identities) == 0 {
		glog.Info("No identities found in store")
		return nil
	}

	glog.Infof("Validating %d identities...", len(config.Identities))

	// Validate each identity
	var validated, failed int
	for _, identity := range config.Identities {
		// Check if user can be retrieved
		user, err := cm.GetUser(ctx, identity.Name)
		if err != nil {
			glog.Errorf("Failed to retrieve user %s: %v", identity.Name, err)
			failed++
			continue
		}

		if user == nil {
			glog.Errorf("User %s not found", identity.Name)
			failed++
			continue
		}

		// Validate access keys
		for _, credential := range identity.Credentials {
			accessKeyUser, err := cm.GetUserByAccessKey(ctx, credential.AccessKey)
			if err != nil {
				glog.Errorf("Failed to retrieve user by access key %s: %v", credential.AccessKey, err)
				failed++
				continue
			}

			if accessKeyUser == nil || accessKeyUser.Name != identity.Name {
				glog.Errorf("Access key %s does not map to correct user %s", credential.AccessKey, identity.Name)
				failed++
				continue
			}
		}

		validated++
		glog.V(1).Infof("Successfully validated user: %s", identity.Name)
	}

	glog.Infof("Validation completed: %d validated, %d failed", validated, failed)

	if failed > 0 {
		return fmt.Errorf("validation completed with %d failures", failed)
	}

	return nil
}