@@ -3,12 +3,14 @@ package publisher
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "encoding/json"
6
7
"errors"
7
8
"fmt"
8
9
"io"
9
10
"os"
10
11
"path"
11
12
"path/filepath"
13
+ "slices"
12
14
"strings"
13
15
"sync"
14
16
"unicode/utf8"
@@ -18,6 +20,7 @@ import (
18
20
"github.com/hashicorp/go-hclog"
19
21
"github.com/hashicorp/vault/sdk/logical"
20
22
23
+ "github.com/werf/logboek"
21
24
"github.com/werf/trdl/server/pkg/config"
22
25
"github.com/werf/trdl/server/pkg/pgp"
23
26
"github.com/werf/trdl/server/pkg/util"
@@ -94,6 +97,9 @@ type setRepositoryKeysOptions struct {
94
97
InitializeKeys bool
95
98
}
96
99
100
+ var TufRepoAlreadyInitializedMsg = `Tuf repository already initialized by another instance of vault-plugin.
101
+ Verify the project settings. If settings are correct, consider cleaning up the TUF repository by removing orphaned metadata.`
102
+
97
103
func (publisher * Publisher ) setRepositoryKeys (ctx context.Context , storage logical.Storage , repository RepositoryInterface , opts setRepositoryKeysOptions ) error {
98
104
entry , err := storage .Get (ctx , storageKeyTufRepositoryKeys )
99
105
if err != nil {
@@ -105,6 +111,16 @@ func (publisher *Publisher) setRepositoryKeys(ctx context.Context, storage logic
105
111
return ErrUninitializedRepositoryKeys
106
112
}
107
113
114
+ rootPublicKeysFromS3 , err := repository .GetRolePublicKeysFromS3Meta ("root.json" , "root" )
115
+ if err != nil {
116
+ return fmt .Errorf ("unable to get root keys from repository: %w" , err )
117
+ }
118
+ if len (rootPublicKeysFromS3 ) > 0 {
119
+ publisher .logger .Error (TufRepoAlreadyInitializedMsg )
120
+ logboek .Context (context .Background ()).Default ().LogF ("%s\n " , TufRepoAlreadyInitializedMsg )
121
+ return fmt .Errorf ("tuf repository already initialized by another instance of vault-plugin" )
122
+ }
123
+
108
124
publisher .logger .Debug ("Will generate new repository private keys" )
109
125
110
126
if err := repository .GenPrivKeys (); err != nil {
@@ -132,6 +148,24 @@ func (publisher *Publisher) setRepositoryKeys(ctx context.Context, storage logic
132
148
return fmt .Errorf ("unable to decode keys json by the %q storage key:\n %s---\n %w" , storageKeyTufRepositoryKeys , entry .Value , err )
133
149
}
134
150
151
+ rootPublicKeysFromS3 , err := repository .GetRolePublicKeysFromS3Meta ("root.json" , "root" )
152
+ if err != nil {
153
+ return fmt .Errorf ("unable to get root keys from repository: %w" , err )
154
+ }
155
+
156
+ var data KeyVal
157
+ if err := json .Unmarshal (privKeys .Root .Value , & data ); err != nil {
158
+ return err
159
+ }
160
+
161
+ if len (rootPublicKeysFromS3 ) > 0 {
162
+ if ! slices .Contains (rootPublicKeysFromS3 , data .Public ) {
163
+ publisher .logger .Error (TufRepoAlreadyInitializedMsg )
164
+ logboek .Context (context .Background ()).Default ().LogF ("%s\n " , TufRepoAlreadyInitializedMsg )
165
+ return fmt .Errorf ("tuf repository already initialized by another instance of vault-plugin" )
166
+ }
167
+ }
168
+
135
169
if err := repository .SetPrivKeys (privKeys ); err != nil {
136
170
return fmt .Errorf ("unable to set private keys into repository: %w" , err )
137
171
}
0 commit comments