Skip to content

Commit

Permalink
Refactor devconn info an interface
Browse files Browse the repository at this point in the history
CL: Refactor devconn info an interface

PUBLISHED_FROM=9f1c97f9d75515acf0210bcd2fcd7bd40c742f58
  • Loading branch information
cpq authored and cesantabot committed Feb 12, 2019
1 parent 91b8f06 commit d8ea7e8
Show file tree
Hide file tree
Showing 34 changed files with 335 additions and 312 deletions.
14 changes: 7 additions & 7 deletions mos/atca.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func getFormat(f, fn string) string {
return f
}

func atcaGetConfig(ctx context.Context, dc *dev.DevConn) error {
func atcaGetConfig(ctx context.Context, dc dev.DevConn) error {
fn := ""
args := flag.Args()
if len(args) == 2 {
Expand Down Expand Up @@ -89,7 +89,7 @@ func atcaGetConfig(ctx context.Context, dc *dev.DevConn) error {
return nil
}

func atcaSetConfig(ctx context.Context, dc *dev.DevConn) error {
func atcaSetConfig(ctx context.Context, dc dev.DevConn) error {
args := flag.Args()
if len(args) < 2 {
return errors.Errorf("config filename is required")
Expand Down Expand Up @@ -161,7 +161,7 @@ func atcaSetConfig(ctx context.Context, dc *dev.DevConn) error {
return nil
}

func atcaLockZone(ctx context.Context, dc *dev.DevConn) error {
func atcaLockZone(ctx context.Context, dc dev.DevConn) error {
args := flag.Args()
if len(args) != 2 {
return errors.Errorf("lock zone name is required (config or data)")
Expand Down Expand Up @@ -268,7 +268,7 @@ func atcaSetECCPrivateKey(slot int64, cfg *atca.Config, data []byte) (*atca.SetK
return req, nil
}

func atcaSetKey(ctx context.Context, dc *dev.DevConn) error {
func atcaSetKey(ctx context.Context, dc dev.DevConn) error {
args := flag.Args()
if len(args) != 3 {
return errors.Errorf("slot number and key filename are required")
Expand Down Expand Up @@ -359,7 +359,7 @@ func writePEM(derBytes []byte, blockType string, outputFileName string) error {
return nil
}

func genCSR(csrTemplateFile string, slot int, dc *dev.DevConn, outputFileName string) error {
func genCSR(csrTemplateFile string, slot int, dc dev.DevConn, outputFileName string) error {
reportf("Generating CSR using template from %s", csrTemplateFile)
data, err := ioutil.ReadFile(csrTemplateFile)
if err != nil {
Expand Down Expand Up @@ -414,7 +414,7 @@ func writePubKey(pubKeyData []byte, outputFileName string) error {
return writePEM(pubKeyDERBytes, "PUBLIC KEY", outputFileName)
}

func atcaGenKey(ctx context.Context, dc *dev.DevConn) error {
func atcaGenKey(ctx context.Context, dc dev.DevConn) error {
args := flag.Args()
if len(args) < 2 {
return errors.Errorf("slot number is required")
Expand Down Expand Up @@ -469,7 +469,7 @@ func atcaGenKey(ctx context.Context, dc *dev.DevConn) error {
}
}

func atcaGetPubKey(ctx context.Context, dc *dev.DevConn) error {
func atcaGetPubKey(ctx context.Context, dc dev.DevConn) error {
args := flag.Args()
if len(args) < 2 {
return errors.Errorf("slot number is required")
Expand Down
4 changes: 2 additions & 2 deletions mos/atca/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
// Implements crypto.Signer interface using ATCA.
type Signer struct {
ctx context.Context
dc *dev.DevConn
dc dev.DevConn
slot int
}

func NewSigner(ctx context.Context, dc *dev.DevConn, slot int) crypto.Signer {
func NewSigner(ctx context.Context, dc dev.DevConn, slot int) crypto.Signer {
return &Signer{ctx: ctx, dc: dc, slot: slot}
}

Expand Down
2 changes: 1 addition & 1 deletion mos/atca/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
KeyFilePrefix = "ATCA:"
)

func Connect(ctx context.Context, dc *dev.DevConn) ([]byte, *Config, error) {
func Connect(ctx context.Context, dc dev.DevConn) ([]byte, *Config, error) {
var r GetConfigResult

if err := dc.Call(ctx, "ATCA.GetConfig", nil, &r); err != nil {
Expand Down
62 changes: 35 additions & 27 deletions mos/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,31 @@ func init() {
MustAsset(rsaCACert)
}

func getSvc() (*iot.IoT, error) {
func getSvc(region, keyID, key string) (*iot.IoT, error) {
sess, err := session.NewSession()
if err != nil {
return nil, errors.Trace(err)
}
cfg := defaults.Get().Config

if AWSRegion == "" {
if region == "" {
output, err := ourutil.GetCommandOutput("aws", "configure", "get", "region")
if err != nil {
if cfg.Region == nil || *cfg.Region == "" {
ourutil.Reportf("Failed to get default AWS region, please specify --aws-region")
return nil, errors.New("AWS region not specified")
} else {
AWSRegion = *cfg.Region
region = *cfg.Region
}
} else {
AWSRegion = strings.TrimSpace(output)
region = strings.TrimSpace(output)
}
}

ourutil.Reportf("AWS region: %s", AWSRegion)
cfg.Region = aws.String(AWSRegion)
ourutil.Reportf("AWS region: %s", region)
cfg.Region = aws.String(region)

creds, err := GetCredentials()
creds, err := GetCredentials(keyID, key)
if err != nil {
// In UI mode, UI credentials are acquired in a different way.
if IsUI {
Expand All @@ -105,7 +105,10 @@ func getSvc() (*iot.IoT, error) {
return iot.New(sess, cfg), nil
}

func GetCredentials() (*credentials.Credentials, error) {
func GetCredentials(keyID, key string) (*credentials.Credentials, error) {
if keyID != "" && key != "" {
return credentials.NewStaticCredentials(keyID, key, ""), nil
}
// Try environment first, fall back to shared.
creds := credentials.NewEnvCredentials()
_, err := creds.Get()
Expand All @@ -132,8 +135,8 @@ func GetRegions() []string {
return regions
}

func GetIoTThings() (string, error) {
iotSvc, err := getSvc()
func GetIoTThings(region, keyID, key string) (string, error) {
iotSvc, err := getSvc(region, keyID, key)
if err != nil {
return "", errors.Trace(err)
}
Expand All @@ -144,8 +147,8 @@ func GetIoTThings() (string, error) {
return things.String(), nil
}

func GetAWSIoTPolicyNames() ([]string, error) {
iotSvc, err := getSvc()
func GetAWSIoTPolicyNames(region, keyID, key string) ([]string, error) {
iotSvc, err := getSvc(region, keyID, key)
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -161,11 +164,11 @@ func GetAWSIoTPolicyNames() ([]string, error) {
return policies, nil
}

func genCert(ctx context.Context, certType x509utils.CertType, useATCA bool, iotSvc *iot.IoT, devConn *dev.DevConn, devConf *dev.DevConf, devInfo *dev.GetInfoResult, cn, thingName string) ([]byte, []byte, error) {
func genCert(ctx context.Context, certType x509utils.CertType, useATCA bool, iotSvc *iot.IoT, devConn dev.DevConn, devConf *dev.DevConf, devInfo *dev.GetInfoResult, cn, thingName, region, policy, keyID, key string) ([]byte, []byte, error) {
var err error

if AWSIoTPolicy == "" {
policies, err := GetAWSIoTPolicyNames()
if policy == "" {
policies, err := GetAWSIoTPolicyNames(region, keyID, key)
if err != nil {
return nil, nil, errors.Trace(err)
}
Expand Down Expand Up @@ -209,9 +212,9 @@ func genCert(ctx context.Context, certType x509utils.CertType, useATCA bool, iot
certPEMBytes := []byte(fmt.Sprintf("CN: %s\r\nID: %s\r\nARN: %s\r\n%s",
cn, *ccResp.CertificateId, *ccResp.CertificateArn, *ccResp.CertificatePem))

if AWSIoTPolicy != awsIoTPolicyNone {
if AWSIoTPolicy == AWSIoTPolicyMOS {
policies, err := GetAWSIoTPolicyNames()
if policy != awsIoTPolicyNone {
if policy == AWSIoTPolicyMOS {
policies, err := GetAWSIoTPolicyNames(region, keyID, key)
if err != nil {
return nil, nil, errors.Trace(err)
}
Expand All @@ -233,7 +236,7 @@ func genCert(ctx context.Context, certType x509utils.CertType, useATCA bool, iot
}
}
}
ourutil.Reportf("Attaching policy %q to the certificate...", AWSIoTPolicy)
ourutil.Reportf("Attaching policy %q to the certificate...", policy)
_, err := iotSvc.AttachPrincipalPolicy(&iot.AttachPrincipalPolicyInput{
PolicyName: aws.String(AWSIoTPolicy),
Principal: ccResp.CertificateArn,
Expand Down Expand Up @@ -313,21 +316,22 @@ func askForCreds() (*credentials.Credentials, error) {
return StoreCreds(ak, sak)
}

func AWSIoTSetup(ctx context.Context, devConn *dev.DevConn) error {
iotSvc, err := getSvc()
func AWSIoTSetupFull(ctx context.Context, devConn dev.DevConn, region, policy, thing, keyID, key string) error {
iotSvc, err := getSvc(region, keyID, key)
if err != nil {
return err
}

ourutil.Reportf("Connecting to the device...")
devInfo, err := devConn.GetInfo(ctx)
devInfo, err := dev.GetInfo(ctx, devConn)
if err != nil {
return errors.Annotatef(err, "failed to connect to device")
}

devArch, devMAC := *devInfo.Arch, *devInfo.Mac
ourutil.Reportf(" %s %s running %s", devArch, devMAC, *devInfo.App)

devConf, err := devConn.GetConfig(ctx)
devConf, err := dev.GetConfig(ctx, devConn)
if err != nil {
return errors.Annotatef(err, "failed to get config")
}
Expand All @@ -352,7 +356,7 @@ func AWSIoTSetup(ctx context.Context, devConn *dev.DevConn) error {
certCN = devID
}

if awsIoTThing == "" {
if thing == "" {
awsIoTThing = certCN
}

Expand All @@ -364,7 +368,7 @@ func AWSIoTSetup(ctx context.Context, devConn *dev.DevConn) error {
_, certPEMBytes, _, _, keyPEMBytes, err := x509utils.LoadCertAndKey(awsCertFile, awsKeyFile)

if certPEMBytes == nil {
certPEMBytes, keyPEMBytes, err = genCert(ctx, certType, useATCA, iotSvc, devConn, devConf, devInfo, certCN, awsIoTThing)
certPEMBytes, keyPEMBytes, err = genCert(ctx, certType, useATCA, iotSvc, devConn, devConf, devInfo, certCN, awsIoTThing, region, policy, keyID, key)
if err != nil {
return errors.Annotatef(err, "failed to generate certificate")
}
Expand Down Expand Up @@ -443,8 +447,8 @@ func AWSIoTSetup(ctx context.Context, devConn *dev.DevConn) error {
settings["device.id"] = certCN
}

if awsIoTThing != "-" {
settings["aws.thing_name"] = awsIoTThing
if thing != "-" {
settings["aws.thing_name"] = thing
}

if awsGGEnable && awsGGConf != "" {
Expand All @@ -460,3 +464,7 @@ func AWSIoTSetup(ctx context.Context, devConn *dev.DevConn) error {

return config.SetAndSave(ctx, devConn, devConf)
}

func AWSIoTSetup(ctx context.Context, devConn dev.DevConn) error {
return AWSIoTSetupFull(ctx, devConn, AWSRegion, AWSIoTPolicy, awsIoTThing, "", "")
}
6 changes: 3 additions & 3 deletions mos/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type azureIoTHubInfo struct {
} `json:"properties"`
}

func AzureIoTSetup(ctx context.Context, devConn *dev.DevConn) error {
func AzureIoTSetup(ctx context.Context, devConn dev.DevConn) error {
// Perform Azure CLI sanity checks
if !azureIoTSkipCLICheck {
// Make sure that Azure CLI is installed and logged in.
Expand Down Expand Up @@ -106,13 +106,13 @@ func AzureIoTSetup(ctx context.Context, devConn *dev.DevConn) error {
ourutil.Reportf("Using IoT hub %s (%s)", azureIoTHubName, azureIoTHubHostName)

ourutil.Reportf("Connecting to the device...")
devInfo, err := devConn.GetInfo(ctx)
devInfo, err := dev.GetInfo(ctx, devConn)
if err != nil {
return errors.Annotatef(err, "failed to connect to device")
}
devArch, devMAC := *devInfo.Arch, *devInfo.Mac
ourutil.Reportf(" %s %s running %s", devArch, devMAC, *devInfo.App)
devConf, err := devConn.GetConfig(ctx)
devConf, err := dev.GetConfig(ctx, devConn)
if err != nil {
return errors.Annotatef(err, "failed to get config")
}
Expand Down
2 changes: 1 addition & 1 deletion mos/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func init() {
// Build {{{

// Build command handler {{{
func buildHandler(ctx context.Context, devConn *dev.DevConn) error {
func buildHandler(ctx context.Context, devConn dev.DevConn) error {
var bParams buildParams
if *buildParamsFlag != "" {
buildParamsBytes, err := ioutil.ReadFile(*buildParamsFlag)
Expand Down
6 changes: 3 additions & 3 deletions mos/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func isJSON(s string) bool {
}

func callDeviceService(
ctx context.Context, devConn *dev.DevConn, method string, args string,
ctx context.Context, devConn dev.DevConn, method string, args string,
) (string, error) {
b, e := devConn.CallB(ctx, method, args)
b, e := devConn.(*dev.MosDevConn).CallB(ctx, method, args)

// TODO(dfrank): instead of that, we should probably add a separate function
// for rebooting
Expand All @@ -36,7 +36,7 @@ func callDeviceService(
return string(b), e
}

func call(ctx context.Context, devConn *dev.DevConn) error {
func call(ctx context.Context, devConn dev.DevConn) error {
args := flag.Args()[1:]
if len(args) < 1 {
return errors.Errorf("method required")
Expand Down
2 changes: 1 addition & 1 deletion mos/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
flag "github.com/spf13/pflag"
)

func Clone(ctx context.Context, devConn *dev.DevConn) error {
func Clone(ctx context.Context, devConn dev.DevConn) error {
var m build.SWModule

args := flag.Args()
Expand Down
14 changes: 7 additions & 7 deletions mos/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
saveAttempts = 3
)

func Get(ctx context.Context, devConn *dev.DevConn) error {
func Get(ctx context.Context, devConn dev.DevConn) error {
path := ""

args := flag.Args()[1:]
Expand All @@ -36,7 +36,7 @@ func Get(ctx context.Context, devConn *dev.DevConn) error {
}

// Get all config from the attached device
devConf, err := devConn.GetConfig(ctx)
devConf, err := dev.GetConfig(ctx, devConn)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -52,20 +52,20 @@ func Get(ctx context.Context, devConn *dev.DevConn) error {
return nil
}

func Set(ctx context.Context, devConn *dev.DevConn) error {
func Set(ctx context.Context, devConn dev.DevConn) error {
return SetWithArgs(ctx, devConn, flag.Args()[1:])
}

func SetWithArgs(
ctx context.Context, devConn *dev.DevConn, args []string,
ctx context.Context, devConn dev.DevConn, args []string,
) error {
if len(args) < 1 {
return errors.Errorf("at least one path.to.value=value pair should be given")
}

// Get all config from the attached device
ourutil.Reportf("Getting configuration...")
devConf, err := devConn.GetConfig(ctx)
devConf, err := dev.GetConfig(ctx, devConn)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -86,10 +86,10 @@ func SetWithArgs(
return SetAndSave(ctx, devConn, devConf)
}

func SetAndSave(ctx context.Context, devConn *dev.DevConn, devConf *dev.DevConf) error {
func SetAndSave(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf) error {
// save changed conf
ourutil.Reportf("Setting new configuration...")
err := devConn.SetConfig(ctx, devConf)
err := dev.SetConfig(ctx, devConn, devConf)
if err != nil {
return errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions mos/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (chr *chanReader) Close() error {
return nil
}

func console(ctx context.Context, devConn *dev.DevConn) error {
func console(ctx context.Context, devConn dev.DevConn) error {

var r io.Reader
var w io.Writer
Expand Down Expand Up @@ -218,7 +218,7 @@ func console(ctx context.Context, devConn *dev.DevConn) error {
if err = devConn.Call(ctx, "Dash.Console.Subscribe", nil, nil); err != nil {
return errors.Trace(err)
}
devConn.RPC.AddHandler("Dash.Console.Event", func(c mgrpc.MgRPC, f *frame.Frame) *frame.Frame {
devConn.(*dev.MosDevConn).RPC.AddHandler("Dash.Console.Event", func(c mgrpc.MgRPC, f *frame.Frame) *frame.Frame {
var ev struct {
DevId string `json:"id"`
Name string `json:"name"`
Expand Down
Loading

0 comments on commit d8ea7e8

Please sign in to comment.