-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CP-24501: add cloudzero-agent-inpector
- Loading branch information
Showing
17 changed files
with
881 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
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,11 @@ | ||
# CloudZero Agent Inspector | ||
|
||
The CloudZero Agent Inspector is a tool that helps you diagnose errors and misconfigurations in your CloudZero Agent configuration. | ||
|
||
## Usage | ||
|
||
The easiest way to use the CloudZero Agent Inspector is to use the [CloudZero Agent Helm chart](https://github.com/Cloudzero/cloudzero-charts/tree/develop/charts/cloudzero-agent). | ||
|
||
However, you can also run the CloudZero Agent Inspector directly from the binary. By default, it will listen on port 9376 and forward all requests to `https://api.cloudzero.com`, though this can be overridden by command line arguments. | ||
|
||
To run the CloudZero Agent Inspector, simply run the executable. Any requests made to the inspector will then be forwarded to the CloudZero API. If the inspector detects errors it will log a description of the error to the console. For common errors, such as an invalid API key, the inspector will include a human-friendly description of the problem. |
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/cloudzero/cloudzero-agent-validator/pkg/build" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cliServerConfig = serverConfig{ | ||
listenPort: 9376, | ||
destinationURL: "https://api.cloudzero.com", | ||
logLevel: zerolog.InfoLevel, | ||
} | ||
|
||
type zerologLevel struct{} | ||
|
||
func (l *zerologLevel) Set(value string) error { | ||
level, err := zerolog.ParseLevel(value) | ||
if err != nil { | ||
return err | ||
} | ||
cliServerConfig.logLevel = level | ||
return nil | ||
} | ||
|
||
func (l *zerologLevel) Type() string { | ||
return "level" | ||
} | ||
|
||
func (l *zerologLevel) String() string { | ||
return cliServerConfig.logLevel.String() | ||
} | ||
|
||
var cliParamLogLevel = zerologLevel{} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "cloudzero-agent-inspector", | ||
Short: "A proxy server for CloudZero API requests", | ||
Long: `cloudzero-agent-inspector acts as a proxy server for CloudZero API requests, allowing inspection and debugging of API traffic.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
Check failure on line 43 in cmd/cloudzero-agent-inspector/cmd.go GitHub Actions / lint
|
||
return runServer(&cliServerConfig) | ||
}, | ||
Version: fmt.Sprintf("%s.%s/%s-%s", build.Rev, build.Tag, runtime.GOOS, runtime.GOARCH), | ||
} | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().Uint16VarP(&cliServerConfig.listenPort, "port", "p", 9376, "Port to listen on") | ||
rootCmd.PersistentFlags().StringVarP(&cliServerConfig.destinationURL, "destination", "d", "https://api.cloudzero.com", "Destination URL to proxy requests to") | ||
rootCmd.PersistentFlags().VarP(&cliParamLogLevel, "log-level", "l", "Log level (panic, fatal, error, warn, info, debug, trace)") | ||
} |
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,76 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/cloudzero/cloudzero-agent-validator/pkg/inspector" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type serverConfig struct { | ||
destinationURL string | ||
listenPort uint16 | ||
logLevel zerolog.Level | ||
} | ||
|
||
func runServer(cfg *serverConfig) error { | ||
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout}).Level(cfg.logLevel) | ||
|
||
targetURL, err := url.Parse(cfg.destinationURL) | ||
if err != nil { | ||
logger.Error().Err(err).Msg("failed to parse target URL") | ||
os.Exit(1) | ||
} | ||
|
||
czInspector := inspector.New() | ||
|
||
proxy := &httputil.ReverseProxy{ | ||
Rewrite: func(pr *httputil.ProxyRequest) { | ||
logger.Debug(). | ||
Str("method", pr.In.Method). | ||
Str("destination", fmt.Sprintf("%s://%s/%s", targetURL.Scheme, targetURL.Host, pr.Out.URL.Path)). | ||
Int64("length", int64(pr.In.ContentLength)). | ||
Msg("rewrite request") | ||
|
||
pr.SetURL(targetURL) | ||
pr.Out.Host = targetURL.Host | ||
}, | ||
ModifyResponse: func(resp *http.Response) error { | ||
return czInspector.Inspect(context.Background(), resp, logger) | ||
}, | ||
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { | ||
logger.Error().Err(err).Msg("proxy error") | ||
w.WriteHeader(http.StatusBadGateway) | ||
}, | ||
} | ||
|
||
server := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", cfg.listenPort), | ||
Handler: proxy, | ||
} | ||
|
||
logger.Info(). | ||
Str("log-level", cfg.logLevel.String()). | ||
Str("addr", server.Addr). | ||
Str("target", targetURL.String()). | ||
Msg("starting inspector server") | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
return fmt.Errorf("failed run server: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
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
Oops, something went wrong.