2022-10-15 16:41:07 +00:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-10 16:56:36 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/garage"
|
2022-10-15 16:41:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
2022-10-15 16:41:07 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
)
|
|
|
|
|
2024-06-17 20:15:28 +00:00
|
|
|
// Paths within garage's global bucket.
|
|
|
|
//
|
|
|
|
// TODO this is getting moved into daemon package.
|
2022-10-15 16:41:07 +00:00
|
|
|
const (
|
|
|
|
garageGlobalBucketBootstrapHostsDirPath = "bootstrap/hosts"
|
|
|
|
)
|
|
|
|
|
2024-06-10 16:56:36 +00:00
|
|
|
// RemoveGarageBootstrapHost removes the <hostname>.json.signed for the given
|
2022-10-29 19:11:40 +00:00
|
|
|
// host from garage.
|
2022-10-15 16:41:07 +00:00
|
|
|
//
|
|
|
|
// The given client should be for the global bucket.
|
|
|
|
func RemoveGarageBootstrapHost(
|
|
|
|
ctx context.Context, client garage.S3APIClient, hostName string,
|
|
|
|
) error {
|
|
|
|
|
2022-10-29 19:11:40 +00:00
|
|
|
filePath := filepath.Join(
|
|
|
|
garageGlobalBucketBootstrapHostsDirPath,
|
2024-06-10 16:56:36 +00:00
|
|
|
hostName+".json.signed",
|
2022-10-29 19:11:40 +00:00
|
|
|
)
|
2022-10-15 16:41:07 +00:00
|
|
|
|
|
|
|
return client.RemoveObject(
|
|
|
|
ctx, garage.GlobalBucket, filePath,
|
|
|
|
minio.RemoveObjectOptions{},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-10 16:56:36 +00:00
|
|
|
// GetGarageBootstrapHosts loads the <hostname>.json.signed file for all hosts
|
2022-10-29 19:11:40 +00:00
|
|
|
// stored in garage.
|
2024-06-17 20:15:28 +00:00
|
|
|
//
|
|
|
|
// Deprecated: should use the method off Daemon instead.
|
2022-10-29 19:11:40 +00:00
|
|
|
func (b Bootstrap) GetGarageBootstrapHosts(
|
|
|
|
ctx context.Context,
|
2022-11-13 15:45:42 +00:00
|
|
|
logger *mlog.Logger,
|
2022-10-15 16:41:07 +00:00
|
|
|
) (
|
|
|
|
map[string]Host, error,
|
|
|
|
) {
|
|
|
|
|
2022-10-29 19:11:40 +00:00
|
|
|
client := b.GlobalBucketS3APIClient()
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
hosts := map[string]Host{}
|
|
|
|
|
|
|
|
objInfoCh := client.ListObjects(
|
|
|
|
ctx, garage.GlobalBucket,
|
|
|
|
minio.ListObjectsOptions{
|
|
|
|
Prefix: garageGlobalBucketBootstrapHostsDirPath,
|
|
|
|
Recursive: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
for objInfo := range objInfoCh {
|
|
|
|
|
2022-11-16 16:27:42 +00:00
|
|
|
ctx := mctx.Annotate(ctx, "objectKey", objInfo.Key)
|
2022-11-13 15:45:42 +00:00
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
if objInfo.Err != nil {
|
|
|
|
return nil, fmt.Errorf("listing objects: %w", objInfo.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := client.GetObject(
|
|
|
|
ctx, garage.GlobalBucket, objInfo.Key, minio.GetObjectOptions{},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("retrieving object %q: %w", objInfo.Key, err)
|
|
|
|
}
|
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
var authedHost AuthenticatedHost
|
2022-10-15 16:41:07 +00:00
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
err = json.NewDecoder(obj).Decode(&authedHost)
|
|
|
|
obj.Close()
|
2022-11-05 14:23:29 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-06-10 20:31:29 +00:00
|
|
|
logger.Warn(ctx, "object contains invalid json", err)
|
2022-10-29 19:11:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-10 20:31:29 +00:00
|
|
|
host, err := authedHost.Unwrap(b.CAPublicCredentials)
|
2022-11-05 14:23:29 +00:00
|
|
|
if err != nil {
|
2024-06-10 20:31:29 +00:00
|
|
|
logger.Warn(ctx, "host could not be authenticated", err)
|
2022-10-29 19:11:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
hosts[host.Name] = host
|
|
|
|
}
|
|
|
|
|
|
|
|
return hosts, nil
|
|
|
|
}
|