-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: topology encryption utils and refactor (#128)
Co-authored-by: Matija Petrunić <matija.petrunic@gmail.com>
- Loading branch information
Showing
18 changed files
with
254 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package topology | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var TopologyCLI = &cobra.Command{ | ||
Use: "topology", | ||
Short: "utility commands that helps to encrypt and test p2p TopologyMap", | ||
} | ||
|
||
func init() { | ||
TopologyCLI.AddCommand(encryptTopologyCMD) | ||
TopologyCLI.AddCommand(testTopologyCMD) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package topology | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/ChainSafe/sygma-relayer/topology" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
encryptTopologyCMD = &cobra.Command{ | ||
Use: "encrypt", | ||
Short: "encrypt provided topology with AES", | ||
Long: "Algorithm used is AES CTR. IV and CT returned are in hex.", | ||
RunE: encryptTopology, | ||
} | ||
) | ||
|
||
var ( | ||
path string | ||
encryptionKey string | ||
) | ||
|
||
func init() { | ||
encryptTopologyCMD.PersistentFlags().StringVar(&path, "path", "", "path to json file with network topology") | ||
_ = encryptTopologyCMD.MarkFlagRequired("path") | ||
encryptTopologyCMD.PersistentFlags().StringVar(&encryptionKey, "encryption-key", "", "password to encrypt topology") | ||
_ = encryptTopologyCMD.MarkFlagRequired("encryption-key") | ||
} | ||
|
||
func encryptTopology(cmd *cobra.Command, args []string) error { | ||
cipherKey := []byte(encryptionKey) | ||
aesEncryption, _ := topology.NewAESEncryption(cipherKey) | ||
topologyFile, err := os.Open(path) | ||
defer func() { | ||
err := topologyFile.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
if err != nil { | ||
return err | ||
} | ||
byteValue, err := ioutil.ReadAll(topologyFile) | ||
if err != nil { | ||
return err | ||
} | ||
// Testing that topology was well-formed | ||
testTopology := topology.RawTopology{} | ||
err = json.Unmarshal(byteValue, &testTopology) | ||
if err != nil { | ||
return fmt.Errorf("topology was wrong formed %s", err.Error()) | ||
} | ||
ct, err := aesEncryption.Encrypt(byteValue) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Encrypted topology is: %x \n", ct) | ||
h := sha256.New() | ||
h.Write(ct) | ||
eh := hex.EncodeToString(h.Sum(nil)) | ||
fmt.Printf("Hash of the topology %s", eh) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package topology | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ChainSafe/sygma-relayer/config/relayer" | ||
"github.com/ChainSafe/sygma-relayer/topology" | ||
) | ||
|
||
var ( | ||
testTopologyCMD = &cobra.Command{ | ||
Use: "test", | ||
Short: "Test topology url", | ||
Long: "CLI tests does provided url contain topology that could be well " + | ||
"decrypted with provided password and then parsed accordingly", | ||
RunE: testTopology, | ||
} | ||
) | ||
|
||
var ( | ||
url string | ||
hash string | ||
decryptionKey string | ||
) | ||
|
||
func init() { | ||
testTopologyCMD.PersistentFlags().StringVar(&decryptionKey, "decryption-key", "", "password to decrypt topology") | ||
_ = testTopologyCMD.MarkFlagRequired("decryption-key") | ||
testTopologyCMD.PersistentFlags().StringVar(&url, "url", "", "url to fetch topology") | ||
_ = testTopologyCMD.MarkFlagRequired("url") | ||
testTopologyCMD.PersistentFlags().StringVar(&hash, "hash", "", "hash of topology") | ||
|
||
} | ||
|
||
func testTopology(cmd *cobra.Command, args []string) error { | ||
config := relayer.TopologyConfiguration{ | ||
EncryptionKey: decryptionKey, | ||
Url: url, | ||
Path: "", | ||
} | ||
nt, err := topology.NewNetworkTopologyProvider(config, http.DefaultClient) | ||
if err != nil { | ||
return err | ||
} | ||
decryptedTopology, err := nt.NetworkTopology(hash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Everything is fine your topology is \n") | ||
fmt.Printf("%+v", decryptedTopology) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.