Skip to content

Commit 59c8b42

Browse files
feat: add oasis rofl set-admin
1 parent 6a30efd commit 59c8b42

6 files changed

Lines changed: 141 additions & 3 deletions

File tree

cmd/rofl/machine/mgmt.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ var (
113113
}
114114

115115
changeAdminCmd = &cobra.Command{
116-
Use: "change-admin [<machine-name> | <provider-address>:<machine-id>] <new-admin>",
117-
Short: "Change the machine administrator",
118-
Args: cobra.RangeArgs(1, 2),
116+
Use: "set-admin [<machine-name> | <provider-address>:<machine-id>] <new-admin>",
117+
Short: "Change the machine administrator",
118+
Aliases: []string{"change-admin"},
119+
Args: cobra.RangeArgs(1, 2),
119120
Run: func(_ *cobra.Command, args []string) {
120121
txCfg := common.GetTransactionConfig()
121122
mCfg, err := resolveMachineCfg(args, &roflCommon.ManifestOptions{

cmd/rofl/rofl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func init() {
2929
Cmd.AddCommand(identityCmd)
3030
Cmd.AddCommand(secretCmd)
3131
Cmd.AddCommand(upgradeCmd)
32+
Cmd.AddCommand(setAdminCmd)
3233
Cmd.AddCommand(provider.Cmd)
3334
Cmd.AddCommand(machine.Cmd)
3435
}

cmd/rofl/set_admin.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package rofl
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
10+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
11+
12+
buildRofl "github.com/oasisprotocol/cli/build/rofl"
13+
"github.com/oasisprotocol/cli/cmd/common"
14+
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
15+
cliConfig "github.com/oasisprotocol/cli/config"
16+
)
17+
18+
var setAdminCmd = &cobra.Command{
19+
Use: "set-admin <new-admin>",
20+
Short: "Change the administrator of the application in ROFL",
21+
Aliases: []string{"change-admin"},
22+
Args: cobra.ExactArgs(1),
23+
Run: func(_ *cobra.Command, args []string) {
24+
txCfg := common.GetTransactionConfig()
25+
26+
manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
27+
NeedAppID: true,
28+
NeedAdmin: true,
29+
})
30+
31+
var appID rofl.AppID
32+
if err := appID.UnmarshalText([]byte(deployment.AppID)); err != nil {
33+
cobra.CheckErr(fmt.Errorf("malformed ROFL app ID: %w", err))
34+
}
35+
36+
npa.MustHaveAccount()
37+
npa.MustHaveParaTime()
38+
39+
if deployment.Policy == nil {
40+
cobra.CheckErr("no policy configured in the manifest")
41+
}
42+
43+
oldAdminAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, deployment.Admin)
44+
if err != nil {
45+
cobra.CheckErr(fmt.Errorf("bad current administrator address: %w", err))
46+
}
47+
48+
newAdminAddr, newAdminEthAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, args[0])
49+
if err != nil {
50+
cobra.CheckErr(fmt.Errorf("invalid new admin address: %w", err))
51+
}
52+
53+
if *oldAdminAddr == *newAdminAddr {
54+
fmt.Println("New admin is the same as the current admin, nothing to do.")
55+
return
56+
}
57+
58+
// When not in offline mode, connect to the given network endpoint.
59+
ctx := context.Background()
60+
var conn connection.Connection
61+
if !txCfg.Offline {
62+
conn, err = connection.Connect(ctx, npa.Network)
63+
cobra.CheckErr(err)
64+
}
65+
66+
newAdminStr := newAdminAddr.String()
67+
if newAdminEthAddr != nil {
68+
newAdminStr = newAdminEthAddr.Hex()
69+
}
70+
71+
fmt.Printf("App ID: %s\n", deployment.AppID)
72+
fmt.Printf("Old admin: %s\n", common.PrettyAddress(oldAdminAddr.String()))
73+
fmt.Printf("New admin: %s\n", common.PrettyAddress(newAdminStr))
74+
75+
secrets := buildRofl.PrepareSecrets(deployment.Secrets)
76+
77+
tx := rofl.NewUpdateTx(nil, &rofl.Update{
78+
ID: appID,
79+
Policy: *deployment.Policy.AsDescriptor(),
80+
Admin: newAdminAddr,
81+
Metadata: manifest.GetMetadata(roflCommon.DeploymentName),
82+
Secrets: secrets,
83+
})
84+
85+
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
86+
sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
87+
cobra.CheckErr(err)
88+
89+
if !common.BroadcastOrExportTransaction(ctx, npa, conn, sigTx, meta, nil) {
90+
return
91+
}
92+
93+
// Transaction succeeded — update the manifest with the new admin.
94+
deployment.Admin = args[0]
95+
if err = manifest.Save(); err != nil {
96+
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
97+
}
98+
99+
fmt.Printf("ROFL admin changed to %s.\n", common.PrettyAddress(newAdminStr))
100+
},
101+
}
102+
103+
func init() {
104+
common.AddAccountFlag(setAdminCmd)
105+
setAdminCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
106+
setAdminCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
107+
}

docs/rofl.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,33 @@ their latest versions. This includes:
325325

326326
![code shell](../examples/rofl/upgrade.in.static)
327327

328+
### Change ROFL app administrator {#set-admin}
329+
330+
Run `rofl set-admin` to transfer ownership of a ROFL app to a new
331+
administrator. The transaction is signed by the current admin and, on success,
332+
the manifest is updated with the new admin.
333+
334+
![code shell](../examples/rofl/set-admin.in.static)
335+
336+
### Change ROFL machine administrator {#machine-set-admin}
337+
338+
Run `rofl machine set-admin` to change the administrator of an individual
339+
machine instance.
340+
341+
![code shell](../examples/rofl/machine-set-admin.in.static)
342+
343+
:::info ROFL admin vs machine admin
344+
345+
The **ROFL admin** (changed via `oasis rofl set-admin`) owns the
346+
application — transfer ownership, upgrades, policy changes, removal.
347+
348+
The **machine admin** (changed via `oasis rofl machine set-admin`) manages
349+
an individual machine instance — execution, restarts, stops.
350+
351+
These are independent roles.
352+
353+
:::
354+
328355
### Remove ROFL app from the network {#remove}
329356

330357
Run `rofl remove` to deregister your ROFL app:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis rofl machine set-admin [<machine-name> | <provider-address>:<machine-id>] <new-admin>

examples/rofl/set-admin.in.static

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis rofl set-admin <new-admin>

0 commit comments

Comments
 (0)