Commit 5c9a10d4 by Domas Committed by GitHub

Logging: sourcemap transform asset urls from CDN in logged stacktraces (#31115)

parent 3303e28b
......@@ -4,6 +4,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"testing"
......@@ -45,8 +46,12 @@ func logSentryEventScenario(t *testing.T, desc string, event frontendlogging.Fro
sc := setupScenarioContext(t, "/log")
cdnRootURL, e := url.Parse("https://storage.googleapis.com/grafana-static-assets")
require.NoError(t, e)
cfg := &setting.Cfg{
StaticRootPath: "/staticroot",
CDNRootURL: cdnRootURL,
}
readSourceMap := func(dir string, path string) ([]byte, error) {
......@@ -252,6 +257,12 @@ func TestFrontendLoggingEndpoint(t *testing.T) {
Lineno: 3,
Colno: 10,
},
{
Function: "cdn",
Filename: "https://storage.googleapis.com/grafana-static-assets/grafana-oss/pre-releases/7.5.0-11925pre/public/build/foo.js", // source map found and mapped
Lineno: 3,
Colno: 10,
},
},
},
},
......@@ -268,8 +279,9 @@ func TestFrontendLoggingEndpoint(t *testing.T) {
at explode (http://localhost:3000/public/build/error.js:3:10)
at wat (http://localhost:3000/public/build/bar.js:3:10)
at nope (http://localhost:3000/baz.js:3:10)
at fake (http://localhost:3000/public/build/../../secrets.txt:3:10)`)
assert.Len(t, sourceMapReads, 5)
at fake (http://localhost:3000/public/build/../../secrets.txt:3:10)
at ? (core|webpack:///./some_source.ts:3:2)`)
assert.Len(t, sourceMapReads, 6)
assert.Equal(t, "/staticroot", sourceMapReads[0].dir)
assert.Equal(t, "build/moo/foo.js.map", sourceMapReads[0].path)
assert.Equal(t, "/usr/local/telepathic-panel", sourceMapReads[1].dir)
......@@ -280,6 +292,8 @@ func TestFrontendLoggingEndpoint(t *testing.T) {
assert.Equal(t, "build/bar.js.map", sourceMapReads[3].path)
assert.Equal(t, "/staticroot", sourceMapReads[4].dir)
assert.Equal(t, "secrets.txt.map", sourceMapReads[4].path)
assert.Equal(t, "/staticroot", sourceMapReads[5].dir)
assert.Equal(t, "build/foo.js.map", sourceMapReads[5].path)
})
})
}
......
......@@ -68,13 +68,16 @@ func (store *SourceMapStore) guessSourceMapLocation(sourceURL string) (*sourceMa
return nil, err
}
// determine if source comes from grafana core, look in public build dir
if strings.HasPrefix(u.Path, "/public/build/") {
return &sourceMapLocation{
dir: store.cfg.StaticRootPath,
path: filepath.Join("build", u.Path[len("/public/build/"):]) + ".map",
pluginID: "",
}, nil
// determine if source comes from grafana core, locally or CDN, look in public build dir on fs
if strings.HasPrefix(u.Path, "/public/build/") || (store.cfg.CDNRootURL != nil && strings.HasPrefix(sourceURL, store.cfg.CDNRootURL.String()) && strings.Contains(u.Path, "/public/build/")) {
pathParts := strings.SplitN(u.Path, "/public/build/", 2)
if len(pathParts) == 2 {
return &sourceMapLocation{
dir: store.cfg.StaticRootPath,
path: filepath.Join("build", pathParts[1]+".map"),
pluginID: "",
}, nil
}
// if source comes from a plugin, look in plugin dir
} else if strings.HasPrefix(u.Path, "/public/plugins/") {
for _, route := range plugins.StaticRoutes {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment