mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #40540 # Checklist for submitter - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Changes present in previous PR ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Updated internal logging infrastructure to improve consistency and maintainability across the application. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package scepclient
|
|
|
|
import (
|
|
"log/slog"
|
|
"time"
|
|
|
|
scepserver "github.com/fleetdm/fleet/v4/server/mdm/scep/server"
|
|
)
|
|
|
|
// Client is a SCEP Client
|
|
type Client interface {
|
|
scepserver.Service
|
|
Supports(capacity string) bool
|
|
}
|
|
|
|
type clientOpts struct {
|
|
timeout *time.Duration
|
|
rootCA string
|
|
insecure bool
|
|
}
|
|
|
|
// Option is a functional option for configuring a SCEP Client
|
|
type Option func(*clientOpts)
|
|
|
|
// WithRootCA sets the root CA file to use when connecting to the SCEP server.
|
|
func WithRootCA(rootCA string) Option {
|
|
return func(c *clientOpts) {
|
|
c.rootCA = rootCA
|
|
}
|
|
}
|
|
|
|
// Insecure configures the client to not verify server certificates.
|
|
// Only used for tests.
|
|
func Insecure() Option {
|
|
return func(c *clientOpts) {
|
|
c.insecure = true
|
|
}
|
|
}
|
|
|
|
// WithTimeout configures the timeout for SCEP client requests.
|
|
func WithTimeout(timeout *time.Duration) Option {
|
|
return func(c *clientOpts) {
|
|
c.timeout = timeout
|
|
}
|
|
}
|
|
|
|
// New creates a SCEP Client.
|
|
func New(
|
|
serverURL string,
|
|
logger *slog.Logger,
|
|
opts ...Option,
|
|
) (Client, error) {
|
|
var co clientOpts
|
|
for _, fn := range opts {
|
|
fn(&co)
|
|
}
|
|
clientOpts := []scepserver.ClientOption{
|
|
scepserver.WithClientTimeout(co.timeout),
|
|
scepserver.WithClientRootCA(co.rootCA),
|
|
}
|
|
if co.insecure {
|
|
clientOpts = append(clientOpts, scepserver.ClientInsecure())
|
|
}
|
|
endpoints, err := scepserver.MakeClientEndpoints(serverURL, clientOpts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
endpoints.GetEndpoint = scepserver.EndpointLoggingMiddleware(logger)(endpoints.GetEndpoint)
|
|
endpoints.PostEndpoint = scepserver.EndpointLoggingMiddleware(logger)(endpoints.PostEndpoint)
|
|
return endpoints, nil
|
|
}
|