mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
* feat: initial implementation of command split * feat: update collect and new send cmd * chore: cleanup unused code * chore: refactored getting username * chore: more refactoring and cleanup * feat: update send cmd implementation * chore: changed/updated example config * chore: made cmd more consistent and added formatting * refactor: removed --host flag from scan * chore: cleaned up and fixed issue with client * chore: cleaned up CLI flags in collect cmd * feat: updated crawl to include managers and output YAML optionally * refactor: updated and improved send implementation * refactor: minor improvements * refactor: added util func to check for empty slices * fix: issue with reading from stdin * refactor: added scheme trimming function for URIs * refactor: changed host arg back to positional * refactor: removed unused vars and added --output-dir flag * fix: make -f for secrets persistent * refactor: removed --host flag and request in collect * refactor: changed --output flag to --output-file * fix: updated flags for collect * fix: typo in crawler error * fix: dir being created when outputDir not set * fix: reading stdin and data args * fix: made output using -v and -o consistent * readme: added info about command split * updated changelog adding missing version entries * chore: updated example to use host as positional arg * fix: issue with reading --data arg * fix: remove unused import from collect pkg Signed-off-by: Devon Bautista <devonb@lanl.gov> --------- Signed-off-by: David Allen <16520934+davidallendj@users.noreply.github.com> Signed-off-by: Devon Bautista <devonb@lanl.gov> Co-authored-by: Devon Bautista <devonb@lanl.gov>
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package client
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type Option[T Client] func(client *T)
|
|
|
|
// The 'Client' struct is a wrapper around the default http.Client
|
|
// that provides an extended API to work with functional options.
|
|
// It also provides functions that work with `collect` data.
|
|
type Client interface {
|
|
Init()
|
|
Name() string
|
|
RootEndpoint(endpoint string) string
|
|
GetInternalClient() *http.Client
|
|
|
|
// functions needed to make request
|
|
Add(data HTTPBody, headers HTTPHeader) error
|
|
Update(data HTTPBody, headers HTTPHeader) error
|
|
}
|
|
|
|
func LoadCertificateFromPath(client Client, path string) error {
|
|
cacert, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read certificate at path: %s", path)
|
|
}
|
|
certPool := x509.NewCertPool()
|
|
certPool.AppendCertsFromPEM(cacert)
|
|
err = LoadCertificateFromPool(client, certPool)
|
|
if err != nil {
|
|
return fmt.Errorf("could not initialize certificate from pool: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func LoadCertificateFromPool(client Client, certPool *x509.CertPool) error {
|
|
// make sure we have a valid cert pool
|
|
if certPool == nil {
|
|
return fmt.Errorf("invalid cert pool")
|
|
}
|
|
|
|
// make sure that we can access the internal client
|
|
internalClient := client.GetInternalClient()
|
|
if internalClient == nil {
|
|
return fmt.Errorf("invalid HTTP client")
|
|
}
|
|
internalClient.Transport = &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
RootCAs: certPool,
|
|
InsecureSkipVerify: false,
|
|
},
|
|
DisableKeepAlives: true,
|
|
Dial: (&net.Dialer{
|
|
Timeout: 120 * time.Second,
|
|
KeepAlive: 120 * time.Second,
|
|
}).Dial,
|
|
TLSHandshakeTimeout: 120 * time.Second,
|
|
ResponseHeaderTimeout: 120 * time.Second,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Post() is a simplified wrapper function that packages all of the
|
|
// that marshals a mapper into a JSON-formatted byte array, and then performs
|
|
// a request to the specified URL.
|
|
func (c *DefaultClient) Post(url string, data map[string]any, header HTTPHeader) (*http.Response, HTTPBody, error) {
|
|
// serialize data into byte array
|
|
body, err := json.Marshal(data)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to marshal data for request: %v", err)
|
|
}
|
|
return MakeRequest(c.Client, url, http.MethodPost, body, header)
|
|
}
|