Skip to content

Commit

Permalink
Eckelj/add cli ta provisioning (#35)
Browse files Browse the repository at this point in the history
* added command line switch --attest-machine-ids-by-file
* related refactoring


Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
  • Loading branch information
eckelj committed Jun 20, 2024
1 parent 9dff968 commit 34a02b9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ A build service can be executed via ```./ta``` or be run via the following go co
go run cmd/ta/main.go
```

The following command will attest a given newline separated file of Trust Wallet machine IDs to the configured network:
```
./ta --attest-machine-ids-by-file keys.txt
```

## Configuration
The service needs to be configured via the ```./app.env``` file or environment variables.
A default configuration file is created at first run.
Expand Down
75 changes: 64 additions & 11 deletions cmd/ta/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"html/template"
"log"
"os"

"github.com/planetmint/planetmint-go/app"
"github.com/planetmint/planetmint-go/lib"
"github.com/rddl-network/ta_attest/config"
"github.com/rddl-network/ta_attest/service"
"github.com/syndtr/goleveldb/leveldb"

"github.com/spf13/viper"
"github.com/syndtr/goleveldb/leveldb"
)

var libConfig *lib.Config

func init() {
encodingConfig := app.MakeEncodingConfig()
libConfig = lib.GetConfig()
libConfig.SetEncodingConfig(encodingConfig)
}

func loadConfig(path string) (cfg *config.Config, err error) {
v := viper.New()
v.AddConfigPath(path)
Expand Down Expand Up @@ -61,26 +72,68 @@ func loadConfig(path string) (cfg *config.Config, err error) {
return
}

func main() {
cfg, err := loadConfig("./")
func attestFileContent(filename string, pmc service.PlanetmintClient) {
// Open the file for reading
file, err := os.Open(filename)
if err != nil {
log.Fatalf("fatal error reading the configuration %s", err)
log.Println("Error opening file:", err)
return
}
defer file.Close() // Ensure file gets closed even in case of errors

db, err := leveldb.OpenFile(cfg.DBPath, nil)
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(file)
log.Println("Start processing the file ...")
// Iterate over each line in the scanner
for scanner.Scan() {
line := scanner.Text()
// Call your attestation function with the current line
log.Println("Attesting : " + line)
err := pmc.AttestTAPublicKeyHex(line)
if err != nil {
log.Println(err.Error())
} else {
log.Println("Successfully attested.")
}
}
log.Println("End of file")
// Handle any errors during scanning
if err := scanner.Err(); err != nil {
log.Println("Error reading file:", err)
}
}

func main() {
cfg, err := loadConfig("./")
if err != nil {
log.Fatalf("fatal error opening db %s", err)
log.Fatalf("fatal error reading the configuration %s", err)
}

libConfig.SetChainID(cfg.PlanetmintChainID)
grpcConn, err := service.SetupGRPCConnection(cfg)
if err != nil {
log.Fatalf("fatal error opening grpc connection %s", err)
}
pmc := service.NewPlanetmintClient(cfg.PlanetmintActor, grpcConn)

TAAttestationService := service.NewTrustAnchorAttestationService(cfg, db, pmc)
err = TAAttestationService.Run()
if err != nil {
fmt.Print(err.Error())
csvFile := flag.String("attest-machine-ids-by-file", "", "Path to a new line separated machine IDs")
flag.Parse()

if *csvFile != "" {
fmt.Println("Attestation mode enabled. Using CSV file:", *csvFile)

attestFileContent(*csvFile, *pmc)
} else {
fmt.Println("Web Service mode")
db, err := leveldb.OpenFile(cfg.DBPath, nil)
if err != nil {
log.Fatalf("fatal error opening db %s", err)
}

TAAttestationService := service.NewTrustAnchorAttestationService(cfg, db, pmc)
err = TAAttestationService.Run()
if err != nil {
fmt.Print(err.Error())
}
}
}
10 changes: 0 additions & 10 deletions service/planetmint_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/planetmint/planetmint-go/app"
"github.com/planetmint/planetmint-go/lib"
machinetypes "github.com/planetmint/planetmint-go/x/machine/types"
"github.com/rddl-network/ta_attest/config"
Expand Down Expand Up @@ -56,15 +55,6 @@ func SetupGRPCConnection(cfg *config.Config) (conn *grpc.ClientConn, err error)
)
}

var libConfig *lib.Config

func init() {
encodingConfig := app.MakeEncodingConfig()

libConfig = lib.GetConfig()
libConfig.SetEncodingConfig(encodingConfig)
}

func (pmc *PlanetmintClient) AttestTAPublicKeyHex(pubHexString string) error {
addr := sdk.MustAccAddressFromBech32(pmc.actor)
msg := machinetypes.NewMsgRegisterTrustAnchor(pmc.actor, &machinetypes.TrustAnchor{
Expand Down
7 changes: 4 additions & 3 deletions service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *TAAService) postPubKey(c *gin.Context) {
pubKey := c.Param("pubkey")
_, err := hex.DecodeString(pubKey)
if err == nil {
fmt.Println(" pub key: " + pubKey)
err = s.pmc.AttestTAPublicKeyHex(pubKey)
if err == nil {
c.IndentedJSON(http.StatusOK, pubKey)
Expand Down Expand Up @@ -110,7 +111,7 @@ func (s *TAAService) createAccount(c *gin.Context) {
taStatus, err := s.pmc.GetTrustAnchorStatus(requestBody.MachineID)
if err != nil {
s.logger.Error("msg", "failed to fetch trust anchor status", "machineID", requestBody.MachineID)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch trust anchor status"})
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to fetch trust anchor status"})
return
}

Expand All @@ -135,8 +136,8 @@ func (s *TAAService) createAccount(c *gin.Context) {

err = s.pmc.FundAccount(requestBody.PlmntAddress)
if err != nil {
s.logger.Error("msg", "failed to send funds", requestBody.PlmntAddress)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send funds"})
s.logger.Error("msg", "failed to send funds ", "address", requestBody.PlmntAddress, "error", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send funds: " + err.Error()})
return
}

Expand Down
2 changes: 1 addition & 1 deletion service/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestPostCreateAccount(t *testing.T) {
Signature: "DE97AEF2A99B9371882C4639A607A11AF2BA8AE520FF7B28203193F5EB63AE1670D431960C3103682901A8F5B3C542139DCF8FB44F97780FC8D8A45F8A4E59E3",
PlmntAddress: "otherPlmntAddr",
},
resBody: "{\"error\":\"failed to send funds\"}",
resBody: "{\"error\":\"failed to send funds: some err\"}",
code: 500,
mocker: func(t *testing.T) *testutil.MockIPlanetmintClient {
ctrl := gomock.NewController(t)
Expand Down
1 change: 0 additions & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type TAAService struct {
}

func NewTrustAnchorAttestationService(cfg *config.Config, db *leveldb.DB, pmc IPlanetmintClient) *TAAService {
libConfig.SetChainID(cfg.PlanetmintChainID)
service := &TAAService{
db: db,
cfg: cfg,
Expand Down

0 comments on commit 34a02b9

Please sign in to comment.