-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC: Auto generation of troubleshooting guide #1223
Draft
doodlesbykumbi
wants to merge
1
commit into
main
Choose a base branch
from
troubleshooting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+510
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
228 changes: 228 additions & 0 deletions
228
test/connector/http/generic/troubleshooting/example_test.go
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,228 @@ | ||
package troubleshooting | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type client func (url, proxy string) (string, error) | ||
|
||
func Code(text string) string { | ||
return "```\n" + text + "\n```" | ||
} | ||
|
||
func Indent(text, indent string) string { | ||
return _Indent(Code(strings.TrimSpace(text)), indent) | ||
} | ||
|
||
// indents a block of text with an indent string | ||
func _Indent(text, indent string) string { | ||
if text[len(text)-1:] == "\n" { | ||
result := "" | ||
for _, j := range strings.Split(text[:len(text)-1], "\n") { | ||
result += indent + j + "\n" | ||
} | ||
return result | ||
} | ||
result := "" | ||
for _, j := range strings.Split(strings.TrimRight(text, "\n"), "\n") { | ||
result += indent + j + "\n" | ||
} | ||
return result[:len(result)-1] | ||
} | ||
|
||
func httpCurl(url, proxy string) (string, error) { | ||
args := []string{ | ||
//"-k", | ||
"-sS", | ||
"-v", | ||
"-X", "GET", | ||
url, | ||
} | ||
|
||
cmd := exec.Command( | ||
"curl", | ||
args..., | ||
) | ||
|
||
proxyVarName := "http_proxy" | ||
if strings.HasPrefix(url, "https") { | ||
proxyVarName = "https_proxy" | ||
} | ||
|
||
cmd.Env = append(cmd.Env, proxyVarName+"="+proxy) | ||
out, err := cmd.CombinedOutput() | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("%s", string(out)) | ||
} | ||
|
||
return string(out), nil | ||
} | ||
|
||
func httpGo(url, proxy string) (string, error) { | ||
res, err := proxyGet( | ||
url, | ||
proxy, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return fmt.Sprintf(` | ||
Status: | ||
%s | ||
|
||
Body: | ||
%s`, res.Status, string(body)), nil | ||
} | ||
|
||
func TestHTTPS(t *testing.T) { | ||
_, logs, err1 := troubleShooting(t, "https://httpbin.org/anything", httpCurl) | ||
if !assert.Error(t, err1) { | ||
return | ||
} | ||
|
||
_, _, err2 := troubleShooting(t, "https://httpbin.org/anything", httpGo) | ||
if !assert.Error(t, err2) { | ||
return | ||
} | ||
|
||
res := fmt.Sprintf(` | ||
### Connecting to Secretless as an HTTPS proxy | ||
#### Symptoms | ||
- You see a CONNECT request in your Secretless logs that looks something like: | ||
|
||
%s | ||
|
||
- Sample client log output messages | ||
|
||
curl: | ||
%s | ||
|
||
Go client: | ||
%s | ||
|
||
#### Known Causes | ||
This type of error occurs when the client attempts to use Secretless as an HTTPS proxy. | ||
Secretless can only act as an HTTP proxy. | ||
This error can happen for a few reasons: | ||
1. An explicit attempt to use Secretless as an HTTPS proxy | ||
1. Providing the client an HTTPS target when intending to proxy the connection through Secretless might result in the client attempting to use Secretless as an HTTPS proxy, as is the case with Go's standard library HTTP client. | ||
|
||
#### Resolution | ||
- Ensure that target of your request is HTTP only e.g. http://httpbin.org. | ||
- Secretless does not support HTTPS between the client and Secretless, it does support it between Secretless and the target. Do not use Secretless as an HTTPS proxy. | ||
- To make connection between Secretless and the target an HTTPS connection you must set "forceSSL: true" on the Secretless service connector config. | ||
|
||
`, Indent(logs.String(), " "), Indent(err1.Error(), " "), Indent(err2.Error(), " ")) | ||
|
||
writeToTroubleshooting(res) | ||
} | ||
|
||
func TestSelfSigned(t *testing.T) { | ||
out1, logs, err := troubleShooting(t, "http://self-signed.badssl.com", httpCurl) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
out2, _, err := troubleShooting(t, "http://wrong.host.badssl.com/", httpGo) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
res := fmt.Sprintf(` | ||
### HTTPS certificate verification failure when forceSSL is set | ||
#### Symptoms | ||
- You see an x509 certificate error in your Secretless logs that looks something like: | ||
|
||
%s | ||
|
||
- Sample client log output messages | ||
|
||
curl: | ||
%s | ||
|
||
Go client: | ||
%s | ||
|
||
#### Known Causes | ||
This type of error occurs when the client attempts to connect to a target with a self-signed certificate, and there is some failure on verification. Secretless verifies all HTTPS connections to the target. | ||
|
||
There are several reasons why verification might fail including: | ||
1. The signer of the target's certificate is not a trusted CA | ||
1. The target's certificate is expired or is not yet valid | ||
1. The target's certificate is not valid for the host | ||
|
||
#### Resolution | ||
|
||
This type of error can be broken into 2 categories. | ||
1. The signer of the target's certificate is not a trusted CA | ||
1. The rest | ||
|
||
For the rest (2), you must ensure that the target's certificate is valid for the target. | ||
|
||
For (1) you will need to ensure that Secretless is aware of the root certificate authority (CA) it should use to verify the server certificates when proxying requests. To do this, ensure that the environment variable **SECRETLESS_HTTP_CA_BUNDLE** is set. **SECRETLESS_HTTP_CA_BUNDLE** is a path to the bundle of CA certificates that are appended to the certificate pool that Secretless uses for server certificate verification of all HTTP service connectors. | ||
`, Indent(logs.String(), " "), Indent(out1, " "), Indent(out2, " ")) | ||
|
||
|
||
writeToTroubleshooting(res) | ||
} | ||
|
||
func writeToTroubleshooting(out string) { | ||
f, err := os.OpenFile("troubleshooting.md", | ||
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
if _, err := f.WriteString(out); err != nil { | ||
panic(err) | ||
} | ||
} | ||
func troubleShooting(t *testing.T, url string, client2 client) (string, bytes.Buffer, error) { | ||
var buf bytes.Buffer | ||
// Create in-process proxy service | ||
proxyService, err := newInProcessProxyService( | ||
[]byte(` | ||
credentialPatterns: | ||
username: '[^:]+' | ||
headers: | ||
Authorization: ' Basic {{ printf "%s:%s" .username .password | base64 }}' | ||
authenticateURLsMatching: | ||
- ^http | ||
forceSSL: true | ||
`), | ||
map[string][]byte{}, | ||
&buf, | ||
) | ||
if !assert.NoError(t, err) { | ||
return "", buf, nil | ||
} | ||
|
||
// Ensure the proxy service is stopped | ||
defer proxyService.Stop() | ||
// Start the proxyService service. Note | ||
proxyService.Start() | ||
|
||
// Avoid all the unnecessary initial logs | ||
buf.Reset(); | ||
|
||
proxyURL := "http://" + proxyService.host + ":" + proxyService.port | ||
|
||
// Make the client request to the proxy service | ||
out, err := client2(url, proxyURL) | ||
return out, buf, err | ||
} |
42 changes: 42 additions & 0 deletions
42
test/connector/http/generic/troubleshooting/http_client_request.go
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,42 @@ | ||
package troubleshooting | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// ephemeralListenerOnPort creates a net.Listener (with a short deadline) on some given | ||
// port. Note that passing in a port of "0" will result in a random port being used. | ||
func ephemeralListenerOnPort(port string) (net.Listener, error) { | ||
listener, err := net.Listen("tcp", "127.0.0.1:"+port) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We generally don't want to wait forever for a connection to come in | ||
//err = listener.(*net.TCPListener).SetDeadline(time.Now().Add(10 * time.Second)) | ||
//if err != nil { | ||
// return nil, err | ||
//} | ||
|
||
return listener, nil | ||
} | ||
|
||
// proxyGet is a convenience method that makes an HTTP GET request using a proxy | ||
func proxyGet(endpoint, proxy string) (*http.Response, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
req, err := http.NewRequest( | ||
"GET", | ||
endpoint, | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
transport := &http.Transport{Proxy: func(req *http.Request) (proxyURL *url.URL, err error) { | ||
return url.Parse(proxy) | ||
}} | ||
client := &http.Client{Transport: transport} | ||
return client.Do(req) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code does not pass gofmt in 6 places. Go fmt your code!