-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from xmidt-org/credentials-improvements
Connect to the test cluster using configuration.
- Loading branch information
Showing
6 changed files
with
202 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,7 @@ internal/jwtxt/cmd/example/* | |
internal/credentials/cmd/example/* | ||
|
||
*.dot | ||
*.pem | ||
*.pem | ||
*.yml | ||
|
||
.storage |
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,92 @@ | ||
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/xmidt-org/xmidt-agent/internal/credentials" | ||
"github.com/xmidt-org/xmidt-agent/internal/credentials/event" | ||
"github.com/xmidt-org/xmidt-agent/internal/fs" | ||
"go.uber.org/fx" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
xmidtProtocol = "protocol" | ||
) | ||
|
||
type credsIn struct { | ||
fx.In | ||
Creds XmidtCredentials | ||
ID Identity | ||
Ops OperationalState | ||
Durable fs.FS `name:"durable_fs" optional:"true"` | ||
LC fx.Lifecycle | ||
Logger *zap.Logger | ||
} | ||
|
||
func provideCredentials(in credsIn) (*credentials.Credentials, error) { | ||
// If the URL is empty, then there is no credentials service to use. | ||
if in.Creds.URL == "" { | ||
return nil, nil | ||
} | ||
|
||
client, err := in.Creds.HTTPClient.NewClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logger := in.Logger.Named("credentials") | ||
|
||
opts := []credentials.Option{ | ||
credentials.URL(in.Creds.URL), | ||
credentials.HTTPClient(client), | ||
credentials.MacAddress(in.ID.DeviceID), | ||
credentials.SerialNumber(in.ID.SerialNumber), | ||
credentials.HardwareModel(in.ID.HardwareModel), | ||
credentials.HardwareManufacturer(in.ID.HardwareManufacturer), | ||
credentials.FirmwareVersion(in.ID.FirmwareVersion), | ||
credentials.PartnerID(func() string { return in.ID.PartnerID }), | ||
credentials.LastRebootReason(in.Ops.LastRebootReason), | ||
credentials.XmidtProtocol(xmidtProtocol), | ||
credentials.BootRetryWait(time.Second), | ||
credentials.RefetchPercent(in.Creds.RefetchPercent), | ||
credentials.AddFetchListener(event.FetchListenerFunc( | ||
func(e event.Fetch) { | ||
logger.Info("fetch", | ||
zap.String("origin", e.Origin), | ||
zap.Time("at", e.At), | ||
zap.Duration("duration", e.Duration), | ||
zap.String("uuid", e.UUID.String()), | ||
zap.Int("status_code", e.StatusCode), | ||
zap.Duration("retry_in", e.RetryIn), | ||
zap.Time("expiration", e.Expiration), | ||
zap.Error(e.Err), | ||
) | ||
})), | ||
} | ||
|
||
if in.Durable != nil { | ||
opts = append(opts, | ||
credentials.LocalStorage(in.Durable, in.Creds.FileName, in.Creds.FilePermissions), | ||
) | ||
} | ||
|
||
creds, err := credentials.New(opts...) | ||
|
||
in.LC.Append(fx.Hook{ | ||
OnStart: func(context.Context) error { | ||
creds.Start() | ||
return nil | ||
}, | ||
OnStop: func(context.Context) error { | ||
creds.Stop() | ||
return nil | ||
}, | ||
}) | ||
|
||
return creds, err | ||
} |
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,34 @@ | ||
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/xmidt-org/xmidt-agent/internal/fs" | ||
"github.com/xmidt-org/xmidt-agent/internal/fs/os" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func fsProvide() fx.Option { | ||
return fx.Provide( | ||
fx.Annotate( | ||
func(s Storage) (fs.FS, fs.FS, error) { | ||
var tmp, durable fs.FS | ||
var err, errs error | ||
|
||
if s.Temporary != "" { | ||
tmp, err = os.New(s.Temporary) | ||
errs = errors.Join(errs, err) | ||
} | ||
if s.Durable != "" { | ||
durable, err = os.New(s.Durable) | ||
errs = errors.Join(errs, err) | ||
} | ||
return tmp, durable, errs | ||
}, | ||
fx.ResultTags(`name:"temporary_fs"`, `name:"durable_fs"`), | ||
), | ||
) | ||
} |
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