-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,255 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/bmc-toolbox/bmclib/v2" | ||
"github.com/bmc-toolbox/bmclib/v2/logging" | ||
"github.com/bmc-toolbox/bmclib/v2/providers/rpc" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
// Start the test consumer | ||
go testConsumer(ctx) | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
log := logging.ZeroLogger("info") | ||
opts := []bmclib.Option{ | ||
bmclib.WithLogger(log), | ||
bmclib.WithPerProviderTimeout(5 * time.Second), | ||
bmclib.WithRPCOpt(rpc.Provider{ | ||
ConsumerURL: "http://localhost:8800", | ||
// Opts are not required. | ||
Opts: rpc.Opts{ | ||
HMAC: rpc.HMACOpts{ | ||
Secrets: rpc.Secrets{rpc.SHA256: {"superSecret1"}}, | ||
}, | ||
Signature: rpc.SignatureOpts{ | ||
HeaderName: "X-Bespoke-Signature", | ||
IncludedPayloadHeaders: []string{"X-Bespoke-Timestamp"}, | ||
}, | ||
Request: rpc.RequestOpts{ | ||
TimestampHeader: "X-Bespoke-Timestamp", | ||
}, | ||
}, | ||
}), | ||
} | ||
host := "127.0.1.1" | ||
user := "admin" | ||
pass := "admin" | ||
c := bmclib.NewClient(host, user, pass, opts...) | ||
if err := c.Open(ctx); err != nil { | ||
panic(err) | ||
} | ||
defer c.Close(ctx) | ||
|
||
state, err := c.GetPowerState(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Info("power state", "state", state) | ||
log.Info("metadata for GetPowerState", "metadata", c.GetMetadata()) | ||
|
||
ok, err := c.SetPowerState(ctx, "on") | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Info("set power state", "ok", ok) | ||
log.Info("metadata for SetPowerState", "metadata", c.GetMetadata()) | ||
|
||
<-ctx.Done() | ||
} | ||
|
||
func testConsumer(ctx context.Context) error { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
req := rpc.RequestPayload{} | ||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
rp := rpc.ResponsePayload{ | ||
ID: req.ID, | ||
Host: req.Host, | ||
} | ||
switch req.Method { | ||
case rpc.PowerGetMethod: | ||
rp.Result = "on" | ||
case rpc.PowerSetMethod: | ||
|
||
case rpc.BootDeviceMethod: | ||
|
||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
b, _ := json.Marshal(rp) | ||
w.Write(b) | ||
}) | ||
|
||
return http.ListenAndServe(":8800", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
Package rpc is a provider that defines an HTTP request/response contract for handling BMC interactions. | ||
It allows users a simple way to interoperate with an existing/bespoke out-of-band management solution. | ||
The rpc provider request/response payloads are modeled after JSON-RPC 2.0, but are not JSON-RPC 2.0 | ||
compliant so as to allow for more flexibility and interoperability with existing systems. | ||
The rpc provider has options that can be set to include an HMAC signature in the request header. | ||
It follows the features found at https://webhooks.fyi/security/hmac, this includes hash algorithms sha256 | ||
and sha512, replay prevention, versioning, and key rotation. | ||
*/ | ||
package rpc |
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,26 @@ | ||
package rpc | ||
|
||
import ( | ||
"github.com/Jeffail/gabs/v2" | ||
"github.com/ghodss/yaml" | ||
) | ||
|
||
// embedPayload will embed the RequestPayload into the given JSON object at the dot path notation location ("object.data"). | ||
func (p *RequestPayload) embedPayload(rawJSON []byte, dotPath string) ([]byte, error) { | ||
if rawJSON == nil { | ||
return rawJSON, nil | ||
} | ||
jdata2, err := yaml.YAMLToJSON(rawJSON) | ||
if err != nil { | ||
return nil, err | ||
} | ||
g, err := gabs.ParseJSON(jdata2) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, err := g.SetP(p, dotPath); err != nil { | ||
return nil, err | ||
} | ||
|
||
return g.Bytes(), nil | ||
} |
Oops, something went wrong.