-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathset_admin.go
More file actions
150 lines (124 loc) · 4.3 KB
/
set_admin.go
File metadata and controls
150 lines (124 loc) · 4.3 KB
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
package rofl
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
buildRofl "github.com/oasisprotocol/cli/build/rofl"
"github.com/oasisprotocol/cli/cmd/common"
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
cliConfig "github.com/oasisprotocol/cli/config"
)
var setAdminCmd = &cobra.Command{
Use: "set-admin [<app-id>] <new-admin>",
Short: "Change the administrator of a ROFL",
Aliases: []string{"change-admin"},
Args: cobra.RangeArgs(1, 2),
Run: func(_ *cobra.Command, args []string) {
cfg := cliConfig.Global()
npa := common.GetNPASelection(cfg)
txCfg := common.GetTransactionConfig()
var (
rawAppID string
newAdminArg string
manifest *buildRofl.Manifest
deployment *buildRofl.Deployment
)
switch len(args) {
case 2:
// Direct mode: oasis rofl set-admin <app-id> <new-admin>
rawAppID = args[0]
newAdminArg = args[1]
case 1:
// Manifest mode: oasis rofl set-admin <new-admin>
newAdminArg = args[0]
manifest, deployment, npa = roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: true,
})
rawAppID = deployment.AppID
}
var appID rofl.AppID
if err := appID.UnmarshalText([]byte(rawAppID)); err != nil {
cobra.CheckErr(fmt.Errorf("malformed ROFL app ID: %w", err))
}
npa.MustHaveAccount()
npa.MustHaveParaTime()
newAdminAddr, newAdminEthAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, newAdminArg)
if err != nil {
cobra.CheckErr(fmt.Errorf("invalid new admin address: %w", err))
}
// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var conn connection.Connection
if !txCfg.Offline {
conn, err = connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)
}
fmt.Printf("App ID: %s\n", appID)
updateBody := rofl.Update{
ID: appID,
Admin: newAdminAddr,
}
var oldAdminAddr *types.Address
if manifest != nil {
// Manifest mode: use local policy, metadata, secrets.
if deployment.Policy == nil {
cobra.CheckErr("no policy configured in the manifest")
}
oldAdminAddr, _, err = common.ResolveLocalAccountOrAddress(npa.Network, deployment.Admin)
if err != nil {
cobra.CheckErr(fmt.Errorf("bad current administrator address: %w", err))
}
updateBody.Policy = *deployment.Policy.AsDescriptor()
updateBody.Metadata = manifest.GetMetadata(roflCommon.DeploymentName)
updateBody.Secrets = buildRofl.PrepareSecrets(deployment.Secrets)
} else {
// Direct mode: reuse current policy, metadata, secrets from chain.
if txCfg.Offline {
cobra.CheckErr("direct mode requires network access")
}
appCfg, err := conn.Runtime(npa.ParaTime).ROFL.App(ctx, client.RoundLatest, appID)
cobra.CheckErr(err)
oldAdminAddr = appCfg.Admin
updateBody.Policy = appCfg.Policy
updateBody.Metadata = appCfg.Metadata
updateBody.Secrets = appCfg.Secrets
}
if oldAdminAddr != nil {
if *oldAdminAddr == *newAdminAddr {
fmt.Println("New admin is the same as the current admin, nothing to do.")
return
}
fmt.Printf("Old admin: %s\n", common.PrettyAddress(oldAdminAddr.String()))
}
newAdminStr := newAdminAddr.String()
if newAdminEthAddr != nil {
newAdminStr = newAdminEthAddr.Hex()
}
fmt.Printf("New admin: %s\n", common.PrettyAddress(newAdminStr))
tx := rofl.NewUpdateTx(nil, &updateBody)
acc := common.LoadAccount(cfg, npa.AccountName)
sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
cobra.CheckErr(err)
if !common.BroadcastOrExportTransaction(ctx, npa, conn, sigTx, meta, nil) {
return
}
// Transaction succeeded — update the manifest if available.
if manifest != nil {
deployment.Admin = newAdminArg
if err = manifest.Save(); err != nil {
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
}
}
fmt.Printf("ROFL admin changed to %s.\n", common.PrettyAddress(newAdminStr))
},
}
func init() {
common.AddSelectorFlags(setAdminCmd)
setAdminCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
setAdminCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
}