Skip to content

Commit db83c8d

Browse files
committed
Add script for reshare migration
1 parent 066d2ca commit db83c8d

3 files changed

Lines changed: 636 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"strings"
7+
8+
"github.com/fystack/mpcium/pkg/config"
9+
"github.com/fystack/mpcium/pkg/infra"
10+
"github.com/fystack/mpcium/pkg/keyinfo"
11+
"github.com/fystack/mpcium/pkg/logger"
12+
"github.com/spf13/viper"
13+
)
14+
15+
type KeyReport struct {
16+
Key string `json:"key"`
17+
WalletID string `json:"wallet_id"`
18+
KeyType string `json:"key_type"`
19+
ParticipantCount int `json:"participant_count"`
20+
ParticipantPeerIDs []string `json:"participant_peer_ids"`
21+
Threshold int `json:"threshold"`
22+
Version int `json:"version"`
23+
}
24+
25+
// extractWalletIDAndKeyType extracts wallet ID and key type from the consul key
26+
// Format: threshold_keyinfo/eddsa:0f2d9b28-7066-4571-855c-980983928fe8:0
27+
// or: threshold_keyinfo/ecdsa:wallet-id:index
28+
func extractWalletIDAndKeyType(consulKey string) (walletID, keyType string) {
29+
// Remove the prefix
30+
withoutPrefix := strings.TrimPrefix(consulKey, "threshold_keyinfo/")
31+
32+
// Split by colon to get key type and wallet info
33+
parts := strings.SplitN(withoutPrefix, ":", 2)
34+
if len(parts) >= 2 {
35+
keyType = parts[0]
36+
walletID = parts[1] // This includes the wallet ID and any suffix like ":0"
37+
} else {
38+
// Fallback: if no colon, treat the whole thing as wallet ID
39+
walletID = withoutPrefix
40+
keyType = "unknown"
41+
}
42+
43+
return walletID, keyType
44+
}
45+
46+
func main() {
47+
config.InitViperConfig()
48+
environment := viper.GetString("environment")
49+
logger.Init(environment, true)
50+
51+
consulClient := infra.GetConsulClient(environment)
52+
// Get KV client
53+
kv := consulClient.KV()
54+
55+
// List all keys under threshold_keyinfo/
56+
pairs, _, err := kv.List("threshold_keyinfo/", nil)
57+
if err != nil {
58+
logger.Fatal("Failed to list keys", err)
59+
}
60+
61+
var keysWithLowParticipants []KeyReport
62+
63+
// Check each key
64+
for _, pair := range pairs {
65+
var info keyinfo.KeyInfo
66+
if err := json.Unmarshal(pair.Value, &info); err != nil {
67+
logger.Warn("Failed to unmarshal key",
68+
"key", pair.Key,
69+
"error", err,
70+
)
71+
continue
72+
}
73+
74+
// Check if participants are less than 3
75+
if len(info.ParticipantPeerIDs) < 3 {
76+
walletID, keyType := extractWalletIDAndKeyType(pair.Key)
77+
78+
report := KeyReport{
79+
Key: pair.Key,
80+
WalletID: walletID,
81+
KeyType: keyType,
82+
ParticipantCount: len(info.ParticipantPeerIDs),
83+
ParticipantPeerIDs: info.ParticipantPeerIDs,
84+
Threshold: info.Threshold,
85+
Version: info.Version,
86+
}
87+
keysWithLowParticipants = append(keysWithLowParticipants, report)
88+
logger.Info("Found key with low participants",
89+
"key", pair.Key,
90+
"wallet_id", walletID,
91+
"key_type", keyType,
92+
"count", len(info.ParticipantPeerIDs),
93+
"participants", info.ParticipantPeerIDs,
94+
)
95+
}
96+
}
97+
98+
// Create report
99+
report := struct {
100+
TotalKeysFound int `json:"total_keys_found"`
101+
Keys []KeyReport `json:"keys"`
102+
}{
103+
TotalKeysFound: len(keysWithLowParticipants),
104+
Keys: keysWithLowParticipants,
105+
}
106+
107+
// Save to JSON file
108+
outputFile := "low_participant_keys.json"
109+
reportJSON, err := json.MarshalIndent(report, "", " ")
110+
if err != nil {
111+
logger.Fatal("Failed to marshal report", err)
112+
}
113+
114+
if err := os.WriteFile(outputFile, reportJSON, 0644); err != nil {
115+
logger.Fatal("Failed to write report", err)
116+
}
117+
118+
logger.Info("Report generated",
119+
"total_keys", len(keysWithLowParticipants),
120+
"output_file", outputFile,
121+
)
122+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# Function to decrypt secrets
4+
decrypt_secret() {
5+
gpg --pinentry-mode loopback --passphrase "$1" -d "$2" 2>/dev/null
6+
}
7+
8+
# Prompt for PASS value
9+
read -sp "Enter the PASS value: " PASS
10+
echo
11+
12+
# Set up environment variables
13+
export GPG_TTY=$(tty)
14+
15+
# Decrypt secrets
16+
NATS_PASSWORD=$(decrypt_secret "$PASS" ~/.password-store/apex-nats-password.gpg)
17+
CONSUL_PASSWORD=$(decrypt_secret "$PASS" ~/.password-store/apex-consul-password.gpg)
18+
CONSUL_TOKEN=$(decrypt_secret "$PASS" ~/.password-store/apex-consul-token.gpg)
19+
BADGER_PASSWORD=$(decrypt_secret "$PASS" ~/.password-store/mpcium-badger-password.gpg)
20+
21+
# Prompt for command
22+
read -p "Enter the command to execute: " user_command
23+
24+
# Execute the command with environment variables
25+
env NATS_PASSWORD="$NATS_PASSWORD" \
26+
CONSUL_PASSWORD="$CONSUL_PASSWORD" \
27+
CONSUL_TOKEN="$CONSUL_TOKEN" \
28+
BADGER_PASSWORD="$BADGER_PASSWORD" \
29+
ENVIRONMENT=production \
30+
$user_command
31+
32+
# Clear sensitive variables
33+
unset PASS NATS_PASSWORD CONSUL_PASSWORD CONSUL_TOKEN BADGER_PASSWORD

0 commit comments

Comments
 (0)