mirror of https://github.com/grafana/grafana
SupportBundles: Add OAuth bundle collectors (#64810)
* wip * add oauth support bundles * add specific configs for generic oauth and azureAD * add doc entry * optimize struct packing * Update pkg/login/social/azuread_oauth.go Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> * nit update --------- Co-authored-by: Ieva <ieva.vasiljeva@grafana.com>pull/64870/head
parent
6d5688ed94
commit
ccbf200c4a
@ -0,0 +1,88 @@ |
||||
package social |
||||
|
||||
import ( |
||||
"bytes" |
||||
"context" |
||||
"fmt" |
||||
"net/http" |
||||
"strings" |
||||
|
||||
"github.com/BurntSushi/toml" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/supportbundles" |
||||
) |
||||
|
||||
func (ss *SocialService) registerSupportBundleCollectors(bundleRegistry supportbundles.Service) { |
||||
for name := range ss.oAuthProvider { |
||||
bundleRegistry.RegisterSupportItemCollector(supportbundles.Collector{ |
||||
UID: "oauth-" + name, |
||||
DisplayName: "OAuth " + strings.Title(strings.ReplaceAll(name, "_", " ")), |
||||
Description: "OAuth configuration and healthchecks for " + name, |
||||
IncludedByDefault: false, |
||||
Default: false, |
||||
Fn: ss.supportBundleCollectorFn(name, ss.socialMap[name], ss.oAuthProvider[name]), |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func (ss *SocialService) supportBundleCollectorFn(name string, sc SocialConnector, oinfo *OAuthInfo) func(context.Context) (*supportbundles.SupportItem, error) { |
||||
return func(ctx context.Context) (*supportbundles.SupportItem, error) { |
||||
bWriter := bytes.NewBuffer(nil) |
||||
|
||||
if _, err := bWriter.WriteString(fmt.Sprintf("# OAuth %s information\n\n", name)); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if _, err := bWriter.WriteString("## Parsed Configuration\n\n"); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
bWriter.WriteString("```toml\n") |
||||
errM := toml.NewEncoder(bWriter).Encode(oinfo) |
||||
if errM != nil { |
||||
bWriter.WriteString( |
||||
fmt.Sprintf("Unable to encode OAuth configuration \n Err: %s", errM)) |
||||
} |
||||
bWriter.WriteString("```\n\n") |
||||
|
||||
if err := sc.SupportBundleContent(bWriter); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
ss.healthCheckSocialConnector(ctx, name, oinfo, bWriter) |
||||
|
||||
return &supportbundles.SupportItem{ |
||||
Filename: "oauth-" + name + ".md", |
||||
FileBytes: bWriter.Bytes(), |
||||
}, nil |
||||
} |
||||
} |
||||
|
||||
func (ss *SocialService) healthCheckSocialConnector(ctx context.Context, name string, oinfo *OAuthInfo, bWriter *bytes.Buffer) { |
||||
bWriter.WriteString("## Health checks\n\n") |
||||
client, err := ss.GetOAuthHttpClient(name) |
||||
if err != nil { |
||||
bWriter.WriteString(fmt.Sprintf("Unable to create HTTP client \n Err: %s\n", err)) |
||||
return |
||||
} |
||||
|
||||
healthCheckEndpoint(client, bWriter, "API", oinfo.ApiUrl) |
||||
healthCheckEndpoint(client, bWriter, "Auth", oinfo.AuthUrl) |
||||
healthCheckEndpoint(client, bWriter, "Token", oinfo.TokenUrl) |
||||
healthCheckEndpoint(client, bWriter, "Teams", oinfo.TeamsUrl) |
||||
} |
||||
|
||||
func healthCheckEndpoint(client *http.Client, bWriter *bytes.Buffer, endpointName string, url string) { |
||||
if url == "" { |
||||
return |
||||
} |
||||
|
||||
bWriter.WriteString(fmt.Sprintf("### %s URL\n\n", endpointName)) |
||||
resp, err := client.Get(url) |
||||
_ = resp.Body.Close() |
||||
if err != nil { |
||||
bWriter.WriteString(fmt.Sprintf("Unable to GET %s URL \n Err: %s\n\n", endpointName, err)) |
||||
} else { |
||||
bWriter.WriteString(fmt.Sprintf("Able to reach %s URL. Status Code does not need to be 200.\n Retrieved Status Code: %d \n\n", endpointName, resp.StatusCode)) |
||||
} |
||||
} |
Loading…
Reference in new issue