mirror of
https://github.com/kairos-io/kcrypt-challenger.git
synced 2025-09-18 16:34:27 +00:00
Extract client code to separate package and test it
- add new suite to the pipeline and fix Earthly to run tests - read configuration from file - the "kcrypt" section is our configuration now - move configuration logic in `kcrypt` repository Part of #399 Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jaypipes/ghw/pkg/block"
|
||||
"github.com/kairos-io/go-tpm"
|
||||
"github.com/kairos-io/kairos/pkg/machine"
|
||||
"github.com/kairos-io/kairos-challenger/cmd/discovery/client"
|
||||
"github.com/kairos-io/kcrypt/pkg/bus"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/mudler/go-pluggable"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) >= 2 && bus.IsEventDefined(os.Args[1]) {
|
||||
checkErr(start())
|
||||
c, err := client.NewClient()
|
||||
checkErr(err)
|
||||
checkErr(c.Start())
|
||||
return
|
||||
}
|
||||
|
||||
pubhash, _ := tpm.GetPubHash()
|
||||
pubhash, err := tpm.GetPubHash()
|
||||
checkErr(err)
|
||||
fmt.Print(pubhash)
|
||||
}
|
||||
|
||||
@@ -29,93 +27,4 @@ func checkErr(err error) {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func readServer() string {
|
||||
connectionDetails := config{}
|
||||
|
||||
var server string
|
||||
// best-effort
|
||||
d, _ := machine.DotToYAML("/proc/cmdline")
|
||||
yaml.Unmarshal(d, &connectionDetails) //nolint:errcheck
|
||||
|
||||
server = connectionDetails.Server
|
||||
if os.Getenv("WSS_SERVER") != "" {
|
||||
server = os.Getenv("WSS_SERVER")
|
||||
}
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
func waitPass(p *block.Partition, attempts int) (pass string, err error) {
|
||||
for tries := 0; tries < attempts; tries++ {
|
||||
server := readServer()
|
||||
if server == "" {
|
||||
err = fmt.Errorf("no server configured")
|
||||
continue
|
||||
}
|
||||
|
||||
pass, err = getPass(server, p)
|
||||
if pass != "" || err == nil {
|
||||
return pass, err
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getPass(server string, partition *block.Partition) (string, error) {
|
||||
msg, err := tpm.Get(server,
|
||||
tpm.WithAdditionalHeader("label", partition.Label),
|
||||
tpm.WithAdditionalHeader("name", partition.Name),
|
||||
tpm.WithAdditionalHeader("uuid", partition.UUID))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
result := map[string]interface{}{}
|
||||
err = json.Unmarshal(msg, &result)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
p, ok := result["passphrase"]
|
||||
if ok {
|
||||
return fmt.Sprint(p), nil
|
||||
}
|
||||
return "", fmt.Errorf("pass for partition not found")
|
||||
}
|
||||
|
||||
type config struct {
|
||||
Server string `yaml:"challenger_server"`
|
||||
}
|
||||
|
||||
// ❯ echo '{ "data": "{ \\"label\\": \\"LABEL\\" }"}' | sudo -E WSS_SERVER="http://localhost:8082/challenge" ./challenger "discovery.password"
|
||||
func start() error {
|
||||
factory := pluggable.NewPluginFactory()
|
||||
|
||||
// Input: bus.EventInstallPayload
|
||||
// Expected output: map[string]string{}
|
||||
factory.Add(bus.EventDiscoveryPassword, func(e *pluggable.Event) pluggable.EventResponse {
|
||||
|
||||
b := &block.Partition{}
|
||||
err := json.Unmarshal([]byte(e.Data), b)
|
||||
if err != nil {
|
||||
return pluggable.EventResponse{
|
||||
Error: fmt.Sprintf("failed reading partitions: %s", err.Error()),
|
||||
}
|
||||
}
|
||||
|
||||
pass, err := waitPass(b, 30)
|
||||
if err != nil {
|
||||
return pluggable.EventResponse{
|
||||
Error: fmt.Sprintf("failed getting pass: %s", err.Error()),
|
||||
}
|
||||
}
|
||||
|
||||
return pluggable.EventResponse{
|
||||
Data: pass,
|
||||
}
|
||||
})
|
||||
|
||||
return factory.Run(pluggable.EventType(os.Args[1]), os.Stdin, os.Stdout)
|
||||
}
|
||||
|
Reference in New Issue
Block a user