/
upload.go
142 lines (119 loc) · 3.94 KB
/
upload.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
// SPDX-License-Identifier: GPL-3.0
package publisher
import (
"context"
"crypto/sha256"
"fmt"
"os"
"github.com/comrade-coop/apocryph/pkg/ipcr"
pb "github.com/comrade-coop/apocryph/pkg/proto"
"github.com/containerd/containerd"
iface "github.com/ipfs/kubo/core/coreiface"
"golang.org/x/exp/slices"
tpipfs "github.com/comrade-coop/apocryph/pkg/ipfs"
)
func UploadSecrets(ctx context.Context, ipfs iface.CoreAPI, basepath string, pod *pb.Pod, deployment *pb.Deployment) error {
fmt.Fprintf(os.Stderr, "Encrypting and uploading secrets to IPFS...\n")
oldUploadedSecrets := map[string]*pb.UploadedSecret{}
for _, uploadedSecret := range deployment.Secrets {
oldUploadedSecrets[uploadedSecret.VolumeName] = uploadedSecret
}
deployment.Secrets = []*pb.UploadedSecret{}
for _, volume := range pod.Volumes {
if volume.Type == pb.Volume_VOLUME_SECRET {
secretBytes, err := tpipfs.ReadSecret(basepath, volume.GetSecret())
if err != nil {
return err
}
secretSha256 := sha256.Sum256(secretBytes)
uploadedSecret, ok := oldUploadedSecrets[volume.Name]
if !ok || !slices.Equal(uploadedSecret.Sha256Sum, secretSha256[:]) {
if ok {
err = tpipfs.RemoveSecret(ctx, ipfs, uploadedSecret.Cid)
if err != nil {
fmt.Printf("Failed unpinning old secret from IPFS: %v", err)
}
}
// Have to reupload...
key, secretContents, err := tpipfs.EncryptSecret(secretBytes)
if err != nil {
return fmt.Errorf("Failed uploading secrets to IPFS: %w", err)
}
secretCid, err := tpipfs.UploadSecret(ctx, ipfs, secretContents)
if err != nil {
return fmt.Errorf("Failed uploading secrets to IPFS: %w", err)
}
uploadedSecret = &pb.UploadedSecret{
VolumeName: volume.Name,
Sha256Sum: secretSha256[:],
Cid: secretCid,
Key: key,
}
fmt.Fprintf(os.Stderr, "Encrypted and uploaded secret `%s` to IPFS\n", volume.Name)
}
deployment.Secrets = append(deployment.Secrets, uploadedSecret)
}
}
return nil
}
func UploadImages(ctx context.Context, client *containerd.Client, IPFSAddress string, pod *pb.Pod, deployment *pb.Deployment) error {
fmt.Fprintf(os.Stderr, "Encrypting and uploading images to IPCR...\n")
oldUploadedImages := map[string]*pb.UploadedImage{}
for _, uploadedImage := range deployment.Images {
oldUploadedImages[uploadedImage.SourceUrl] = uploadedImage
}
deployment.Images = []*pb.UploadedImage{}
for _, container := range pod.Containers {
image := container.Image
err := ipcr.EnsureImage(ctx, client, image.Url)
if err != nil {
return err
}
_, prvKey, err := ipcr.EncryptImage(ctx, client, image.Url, "")
if err != nil {
return err
}
cid, err := ipcr.PushImage(ctx, client, IPFSAddress, image.Url)
if err != nil {
return err
}
uploadedImage := &pb.UploadedImage{SourceUrl: image.Url, Cid: []byte(cid), Key: &pb.Key{Data: prvKey}}
deployment.Images = append(deployment.Images, uploadedImage)
}
return nil
}
func LinkUploadsFromDeployment(pod *pb.Pod, deployment *pb.Deployment) *pb.Pod {
if pod == nil {
return nil
}
uploadedSecrets := map[string]*pb.UploadedSecret{}
for _, uploadedSecret := range deployment.Secrets {
uploadedSecrets[uploadedSecret.VolumeName] = uploadedSecret
}
for _, volume := range pod.Volumes {
if volume.Type == pb.Volume_VOLUME_SECRET {
if uploadedSecret, ok := uploadedSecrets[volume.Name]; ok {
volume.Configuration = &pb.Volume_Secret{
Secret: &pb.Volume_SecretConfig{
Cid: uploadedSecret.Cid,
Key: uploadedSecret.Key,
},
}
}
}
}
uploadedImages := map[string]*pb.UploadedImage{}
for _, uploadedImage := range deployment.Images {
uploadedImages[uploadedImage.SourceUrl] = uploadedImage
}
for _, container := range pod.Containers {
if uploadedImage, ok := uploadedImages[container.Image.Url]; ok {
container.Image = &pb.Container_Image{
Cid: uploadedImage.Cid,
Url: uploadedImage.SourceUrl,
Key: uploadedImage.Key,
}
}
}
return pod
}