forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace machine-id errors by configuration hint (evcc-io#4591)
- Loading branch information
Showing
10 changed files
with
115 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package machine | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/evcc-io/evcc/util" | ||
|
||
"github.com/denisbrodbeck/machineid" | ||
) | ||
|
||
var id string | ||
|
||
// CustomID sets the machine id to a custom value | ||
func CustomID(cid string) error { | ||
if id != "" { | ||
panic("machine id already generated") | ||
} | ||
|
||
cid = strings.TrimSpace(cid) | ||
if l := len(cid); l != 32 { | ||
return fmt.Errorf("expected 32 characters machine id, got %d", l) | ||
} | ||
|
||
id = cid | ||
|
||
return nil | ||
} | ||
|
||
// ID returns the platform specific machine id of the current host OS. | ||
// If ID cannot be generated, a random value is suggested. | ||
func ID() (string, error) { | ||
if id == "" { | ||
var err error | ||
if id, err = machineid.ID(); err != nil { | ||
rnd := util.RandomString(512) | ||
mac := hmac.New(sha256.New, []byte(rnd)) | ||
rid := hex.EncodeToString(mac.Sum(nil)) | ||
|
||
return "", fmt.Errorf("could not get %w; for manual configuration use plant: %s", err, rid) | ||
} | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
// ProtectedID returns a hashed version of the machine id | ||
// using a fixed, application-specific key. | ||
func ProtectedID(key string) (string, error) { | ||
id, err := machineid.ID() | ||
if err != nil { | ||
return id, err | ||
} | ||
|
||
return protect(key, id), nil | ||
} | ||
|
||
// protect calculates HMAC-SHA256 of the id, keyed by key and returns a hex-encoded string | ||
func protect(key, id string) string { | ||
mac := hmac.New(sha256.New, []byte(id)) | ||
mac.Write([]byte(key)) | ||
return hex.EncodeToString(mac.Sum(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,22 @@ | ||
package machine | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/denisbrodbeck/machineid" | ||
) | ||
|
||
func TestProtectedMachineId(t *testing.T) { | ||
const key = "foo" | ||
|
||
if mid, err := machineid.ProtectedID(key); err == nil { | ||
id, err := ProtectedID(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if mid != id { | ||
t.Errorf("machine id mismatch. expected %s, got %s", mid, id) | ||
} | ||
} | ||
} |
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