Persist Datadog domain settings on reconnect
Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
@@ -65,6 +65,12 @@ type (
|
|||||||
OrganizationID gid.GID
|
OrganizationID gid.GID
|
||||||
Provider coredata.ConnectorProvider
|
Provider coredata.ConnectorProvider
|
||||||
Connection connector.Connection
|
Connection connector.Connection
|
||||||
|
// RawSettings, when non-empty, replaces the connector's
|
||||||
|
// provider-specific settings on reconnect. Datadog captures its
|
||||||
|
// per-customer API domain on every OAuth callback (the domain
|
||||||
|
// drives the driver's API host), so a reconnect must refresh it;
|
||||||
|
// empty leaves the existing settings intact.
|
||||||
|
RawSettings json.RawMessage
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -356,6 +362,11 @@ func (s *ConnectorService) Reconnect(
|
|||||||
|
|
||||||
preserveConnectionFields(req.Connection, cnnctr.Connection)
|
preserveConnectionFields(req.Connection, cnnctr.Connection)
|
||||||
cnnctr.Connection = req.Connection
|
cnnctr.Connection = req.Connection
|
||||||
|
|
||||||
|
if len(req.RawSettings) > 0 {
|
||||||
|
cnnctr.RawSettings = []byte(req.RawSettings)
|
||||||
|
}
|
||||||
|
|
||||||
cnnctr.UpdatedAt = time.Now()
|
cnnctr.UpdatedAt = time.Now()
|
||||||
|
|
||||||
return cnnctr.Update(ctx, conn, scope, s.svc.encryptionKey)
|
return cnnctr.Update(ctx, conn, scope, s.svc.encryptionKey)
|
||||||
|
|||||||
@@ -189,6 +189,40 @@ func handleConnectorComplete(
|
|||||||
|
|
||||||
var cnnctr *coredata.Connector
|
var cnnctr *coredata.Connector
|
||||||
|
|
||||||
|
// Datadog returns the customer's API domain as a `domain` query
|
||||||
|
// parameter on every OAuth callback; it drives the driver's API
|
||||||
|
// host, so capture and validate it on both the create and the
|
||||||
|
// reconnect path (a reconnect from a different site must refresh
|
||||||
|
// the stored domain).
|
||||||
|
var datadogRawSettings json.RawMessage
|
||||||
|
|
||||||
|
if connectorProvider == coredata.ConnectorProviderDatadog {
|
||||||
|
domain := query.Get("domain")
|
||||||
|
if !connector.IsValidDatadogDomain(domain) {
|
||||||
|
logger.WarnCtx(r.Context(), "rejecting invalid datadog domain",
|
||||||
|
log.String("provider", string(connectorProvider)),
|
||||||
|
)
|
||||||
|
httpserver.RenderError(w, http.StatusBadRequest, fmt.Errorf("invalid domain"))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
region, _ := connector.DatadogSiteForDomain(domain)
|
||||||
|
|
||||||
|
raw, err := json.Marshal(&coredata.DatadogConnectorSettings{
|
||||||
|
Region: region,
|
||||||
|
Domain: domain,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.ErrorCtx(r.Context(), "cannot marshal datadog settings", log.Error(err))
|
||||||
|
httpserver.RenderError(w, http.StatusInternalServerError, fmt.Errorf("internal error"))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
datadogRawSettings = raw
|
||||||
|
}
|
||||||
|
|
||||||
// If a connector_id was passed in the state, this is a
|
// If a connector_id was passed in the state, this is a
|
||||||
// reconnection — update the existing connector's token.
|
// reconnection — update the existing connector's token.
|
||||||
if state.ConnectorID != "" {
|
if state.ConnectorID != "" {
|
||||||
@@ -206,6 +240,7 @@ func handleConnectorComplete(
|
|||||||
OrganizationID: organizationID,
|
OrganizationID: organizationID,
|
||||||
Provider: connectorProvider,
|
Provider: connectorProvider,
|
||||||
Connection: connection,
|
Connection: connection,
|
||||||
|
RawSettings: datadogRawSettings,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -293,35 +328,11 @@ func handleConnectorComplete(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Datadog returns the customer's API domain as a `domain`
|
// Datadog's per-customer settings were captured and validated
|
||||||
// query parameter on the callback (CompleteWithState has
|
// above (the same block also feeds the reconnect path); apply
|
||||||
// already validated it by building the token URL from it).
|
// them to the create request.
|
||||||
// Re-validate defensively, reverse-map to the site key, and
|
if datadogRawSettings != nil {
|
||||||
// persist both on the connector settings.
|
createReq.RawSettings = datadogRawSettings
|
||||||
if connectorProvider == coredata.ConnectorProviderDatadog {
|
|
||||||
domain := query.Get("domain")
|
|
||||||
if !connector.IsValidDatadogDomain(domain) {
|
|
||||||
logger.WarnCtx(r.Context(), "rejecting invalid datadog domain",
|
|
||||||
log.String("provider", string(connectorProvider)),
|
|
||||||
)
|
|
||||||
httpserver.RenderError(w, http.StatusBadRequest, fmt.Errorf("invalid domain"))
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
region, _ := connector.DatadogSiteForDomain(domain)
|
|
||||||
raw, err := json.Marshal(&coredata.DatadogConnectorSettings{
|
|
||||||
Region: region,
|
|
||||||
Domain: domain,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
logger.ErrorCtx(r.Context(), "cannot marshal datadog settings", log.Error(err))
|
|
||||||
httpserver.RenderError(w, http.StatusInternalServerError, fmt.Errorf("internal error"))
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
createReq.RawSettings = raw
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cnnctr, err = svc.Connectors.Create(r.Context(), scope, createReq)
|
cnnctr, err = svc.Connectors.Create(r.Context(), scope, createReq)
|
||||||
|
|||||||
Reference in New Issue
Block a user