Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions cmd/smoke/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ type probe struct {
// needs as the dhm.APIKeyParam query parameter (#1065); "" for none.
// It is appended at send time, so the printed url never holds it.
apiKey string
// clientKey is the config key of a browser key the request needs inside
// its URL (#1065 M6: Skærmkort, whose URL the browser builds with the
// key first); "" for none. The key is only ever in the sent URL: the
// printed one is built with a placeholder.
clientKey string
// urlKeyed builds the URL of a clientKey probe from the key.
urlKeyed func(key string) (string, error)
// url builds the request URL with the client's own builder.
url func() (string, error)
// rng, when set, is sent as the Range header.
Expand Down Expand Up @@ -113,14 +120,18 @@ func execute(ctx context.Context, hc *http.Client, p probe, toks tokens) result
res.verdict, res.err = skip, p.skip
return res
}
for _, k := range []string{p.token, p.apiKey} {
for _, k := range []string{p.token, p.apiKey, p.clientKey} {
if k != "" && toks[k] == "" {
res.verdict, res.err = skip, "no "+k+" in the config"
return res
}
}
res.verdict = fail
target, err := p.url()
build := p.url
if p.clientKey != "" {
build = func() (string, error) { return p.urlKeyed(toks[p.clientKey]) }
}
target, err := build()
var se skipError
if errors.As(err, &se) {
res.verdict, res.err = skip, se.Error()
Expand All @@ -131,6 +142,13 @@ func execute(ctx context.Context, hc *http.Client, p probe, toks tokens) result
return res
}
res.url = target
if p.clientKey != "" {
// The printed URL never holds the key, not even redacted.
if res.url, err = p.urlKeyed(redacted); err != nil {
res.err = "building the request: " + err.Error()
return res
}
}
if p.apiKey != "" {
target += "&" + dhm.APIKeyParam + "=" + url.QueryEscape(toks[p.apiKey])
}
Expand Down Expand Up @@ -180,6 +198,8 @@ func execute(ctx context.Context, hc *http.Client, p probe, toks tokens) result
// (#1065 M0 §5.4): which credential to check, never what upstream said.
func refusalHint(p probe, status int) string {
switch {
case p.clientKey != "":
return "Datafordeleren refused the browser key: " + p.clientKey + " is missing or invalid, younger than 15 minutes, or its IT-system lacks the Skærmkort WMS"
case p.apiKey != "" && status == http.StatusUnauthorized:
return "Datafordeleren refused the key: " + p.apiKey + " is missing or invalid, or younger than 15 minutes (a new key takes 15 minutes to become active)"
case p.apiKey != "":
Expand Down
24 changes: 23 additions & 1 deletion cmd/smoke/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const tokenDHM = "map.dhm.token"
// the apikey query parameter.
const keyDatafordeler = "map.datafordeler.apiKey"

// keyClientSkaermkort is the config key of the browser key of the Skærmkort
// base maps (#1065 M6), a key of its own from a separate IT-system, never
// map.datafordeler.apiKey. The Skærmkort probe sends it as the browser does.
const keyClientSkaermkort = "map.tiles.providers.datafordeler.apiKey"

// tokens are the tokens read from the config, by config key; a missing
// or blank one is "".
type tokens map[string]string
Expand Down Expand Up @@ -51,7 +56,7 @@ func loadTokens(path string) (tokens, error) {
if err := json.Unmarshal(data, &doc); err != nil {
return nil, fmt.Errorf("config %s: %s", path, describeDecodeError(err))
}
toks := tokens{tokenDHM: "", keyDatafordeler: ""}
toks := tokens{tokenDHM: "", keyDatafordeler: "", keyClientSkaermkort: clientSkaermkortKey(doc.Map["tiles"])}
if raw, ok := doc.Map["datafordeler"]; ok {
var d dhm.DatafordelerConfig
if err := json.Unmarshal(raw, &d); err != nil {
Expand Down Expand Up @@ -84,3 +89,20 @@ func describeDecodeError(err error) string {
}
return "it could not be decoded as JSON"
}

// clientSkaermkortKey is map.tiles.providers.datafordeler.apiKey in raw, trimmed;
// "" for anything missing or of another shape (the browser ignores such a
// block too, so it is a SKIP, not an error).
func clientSkaermkortKey(raw json.RawMessage) string {
var tiles struct {
Providers struct {
Datafordeler struct {
APIKey string `json:"apiKey"`
} `json:"datafordeler"`
} `json:"providers"`
}
if raw == nil || json.Unmarshal(raw, &tiles) != nil {
return ""
}
return strings.TrimSpace(tiles.Providers.Datafordeler.APIKey)
}
19 changes: 11 additions & 8 deletions cmd/smoke/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ var site = dhm.LatLon{Lat: 55.6761, Lon: 12.5683}
// overlay's range, contours (from zoom 14) included, and the ortho's.
const siteZoom = 14

// skaermkortSkip is why Skærmkort is not probed: #741 builds its request
// in the browser only, and the smoke check sends no hand-written URL.
const skaermkortSkip = "no Go request builder: the Skærmkort WMS URL is built in the browser " +
"(public/map-tile-providers.js, #741; on Datafordeleren since #1065 M6), with the browser key " +
"map.tiles.providers.datafordeler.apiKey; " +
"probe it here once a Go builder exists"

// naturSkip is why Natur- og Friluftskort is not probed, for the same
// reason as Skærmkort (#741 M2).
const naturSkip = "no Go request builder: the Natur- og Friluftskort WMS URL is built in the browser " +
Expand Down Expand Up @@ -267,7 +260,17 @@ func buildProbes(s settings) []probe {
check: checkRange(cdnRange, checkTIFF),
})

ps = append(ps, probe{name: "skaermkort-wms", skip: skaermkortSkip})
// Skærmkort (#1065 M6): the browser's own request, with the browser key.
sk := siteTileAt(skaermkortZoom)
ps = append(ps, probe{
name: "skaermkort-wms",
clientKey: keyClientSkaermkort,
urlKeyed: func(key string) (string, error) {
return skaermkortURL(key, skaermkortLayer, sk.Z, sk.X, sk.Y), nil
},
status: http.StatusOK,
check: checkImage("png", skaermkortTileSize),
})
ps = append(ps, probe{name: "natur-friluft-wms", skip: naturSkip})
return ps
}
62 changes: 62 additions & 0 deletions cmd/smoke/skaermkort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"math"
"net/url"
"strconv"
"strings"
)

// The Skærmkort WMS request, as the browser builds it (#1065 M6,
// #1081 review F2): public/map-tile-providers.js gives Leaflet's WMS layer
// the base URL <service>?apikey=<key>, the layer and 512 px tiles with
// zoomOffset -1, and Leaflet appends the GetMap parameters in its own
// order and the tile's EPSG:3857 box last. skaermkortURL reproduces that
// string exactly; test-fixtures/skaermkort-wms-url-1065.json holds what the
// browser requested, and both sides assert it, so the two cannot drift.

// skaermkortService is the Datafordeleren WMS of the Skærmkort layers.
const skaermkortService = "https://wms.datafordeler.dk/Dkskaermkort/topo_skaermkort/1.0.0/wms"

// skaermkortLayer is the standard Skærmkort; the grå and dæmpet variants
// are dtk_skaermkort_graa and dtk_skaermkort_daempet.
const skaermkortLayer = "dtk_skaermkort"

// skaermkortZoom is the tile zoom of the probe: the browser's 512 px tiles
// have zoomOffset -1, so map zoom siteZoom asks tiles of zoom siteZoom-1.
const skaermkortZoom = siteZoom - 1

// skaermkortTileSize is the WMS tile edge in pixels.
const skaermkortTileSize = 512

// webMercatorHalfWorld is half the EPSG:3857 world width in metres
// (π × the WGS 84 radius).
const webMercatorHalfWorld = math.Pi * 6378137

// skaermkortURL is the GetMap URL of tile z/x/y (the browser's 512 px tile
// grid) of layer, with key as the apikey parameter.
func skaermkortURL(key, layer string, z, x, y int) string {
res := 2 * webMercatorHalfWorld / math.Exp2(float64(z))
minX := -webMercatorHalfWorld + float64(x)*res
maxY := webMercatorHalfWorld - float64(y)*res
box := []string{formatMetres(minX), formatMetres(maxY - res), formatMetres(minX + res), formatMetres(maxY)}
edge := strconv.Itoa(skaermkortTileSize)
return skaermkortService + "?apikey=" + encodeURIComponent(key) +
"&service=WMS&request=GetMap&layers=" + encodeURIComponent(layer) +
"&styles=&format=image%2Fpng&transparent=FALSE&version=1.3.0" +
"&width=" + edge + "&height=" + edge + "&crs=EPSG%3A3857&bbox=" + strings.Join(box, ",")
}

// formatMetres is the shortest decimal that parses back to v, like
// JavaScript's Number toString for these magnitudes.
func formatMetres(v float64) string { return strconv.FormatFloat(v, 'f', -1, 64) }

// encodeURIComponent is JavaScript's: everything but A-Z a-z 0-9 - _ . ! ~ * ' ( )
// is percent-escaped, a space as %20.
func encodeURIComponent(s string) string {
s = strings.ReplaceAll(url.QueryEscape(s), "+", "%20")
for _, r := range []struct{ from, to string }{{"%21", "!"}, {"%27", "'"}, {"%28", "("}, {"%29", ")"}, {"%2A", "*"}} {
s = strings.ReplaceAll(s, r.from, r.to)
}
return s
}
Loading
Loading