Skip to content

Commit

Permalink
Merge pull request #263 from PDOK/pdok-17366-collection-id-regex
Browse files Browse the repository at this point in the history
restrict collection ID format
  • Loading branch information
kad-dirc authored Dec 18, 2024
2 parents 5f45017 + 85885fb commit 0ce5211
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 57 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM docker.io/node:lts-alpine3.17 AS build-component
RUN mkdir -p /usr/src/app
COPY ./viewer /usr/src/app
WORKDIR /usr/src/app
RUN npm config set registry http://registry.npmjs.org
RUN npm install
RUN npm run build

Expand Down
6 changes: 3 additions & 3 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func Test_newRouter(t *testing.T) {
{
name: "Serve multiple OGC APIs for single collection in JSON",
configFile: "internal/engine/testdata/config_multiple_ogc_apis_single_collection.yaml",
apiCall: "http://localhost:8180/collections/NewYork?f=json",
apiCall: "http://localhost:8180/collections/newyork?f=json",
wantBody: "internal/engine/testdata/expected_multiple_ogc_apis_single_collection.json",
},
{
name: "Serve multiple OGC APIs for single collection in HTML",
configFile: "internal/engine/testdata/config_multiple_ogc_apis_single_collection.yaml",
apiCall: "http://localhost:8180/collections/NewYork?f=html",
apiCall: "http://localhost:8180/collections/newyork?f=html",
wantBody: "internal/engine/testdata/expected_multiple_ogc_apis_single_collection.html",
},
{
name: "Serve JSON-LD in multiple OGC APIs for single collection in HTML",
configFile: "internal/engine/testdata/config_multiple_ogc_apis_single_collection.yaml",
apiCall: "http://localhost:8180/collections/NewYork?f=html",
apiCall: "http://localhost:8180/collections/newyork?f=html",
wantBody: "internal/engine/testdata/expected_multiple_ogc_apis_single_collection_json_ld.html",
},
{
Expand Down
3 changes: 2 additions & 1 deletion config/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type GeoSpatialCollections []GeoSpatialCollection
// +kubebuilder:object:generate=true
type GeoSpatialCollection struct {
// Unique ID of the collection
ID string `yaml:"id" validate:"required" json:"id"`
// +kubebuilder:validation:Pattern=`^[a-z0-9]([a-z0-9_-]*[a-z0-9]+|)$`
ID string `yaml:"id" validate:"required,lowercase_id" json:"id"`

// Metadata describing the collection contents
// +optional
Expand Down
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ func setHealthCheckTilePath(tilesConfig *Tiles) {
func validate(config *Config) error {
// process 'validate' tags
v := validator.New()
err := v.Struct(config)
err := RegisterAllValidators(v)
if err != nil {
return err
}
err = v.Struct(config)
if err != nil {
var ive *validator.InvalidValidationError
if ok := errors.Is(err, ive); ok {
Expand Down
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,28 @@ func TestGeoSpatialCollections_Ordering(t *testing.T) {
name: "should return collections in default order (alphabetic)",
args: args{
configFile: "internal/engine/testdata/config_collections_order_alphabetic.yaml",
expectedOrder: []string{"A", "B", "C", "Z", "Z"},
expectedOrder: []string{"a", "b", "c", "z", "z"},
},
},
{
name: "should return collections in default order (alphabetic) - by title",
args: args{
configFile: "internal/engine/testdata/config_collections_order_alphabetic_titles.yaml",
expectedOrder: []string{"B", "C", "Z", "Z", "A"},
expectedOrder: []string{"b", "c", "z", "z", "a"},
},
},
{
name: "should return collections in default order (alphabetic) - extra test",
args: args{
configFile: "internal/engine/testdata/config_collections_unique.yaml",
expectedOrder: []string{"BarCollection", "FooCollection", "FooCollection"},
expectedOrder: []string{"bar_collection", "foo_collection", "foo_collection"},
},
},
{
name: "should return collections in explicit / literal order",
args: args{
configFile: "internal/engine/testdata/config_collections_order_literal.yaml",
expectedOrder: []string{"Z", "Z", "C", "A", "B"},
expectedOrder: []string{"z", "z", "c", "a", "b"},
},
},
{
Expand Down
3 changes: 2 additions & 1 deletion config/ogcapi_styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type OgcAPIStyles struct {
// +kubebuilder:object:generate=true
type Style struct {
// Unique ID of this style
ID string `yaml:"id" json:"id" validate:"required"`
// +kubebuilder:validation:Pattern=`^[a-z0-9]([a-z0-9_-]*[a-z0-9]+|)$`
ID string `yaml:"id" json:"id" validate:"required,lowercase_id"`

// Human-friendly name of this style
Title string `yaml:"title" json:"title" validate:"required"`
Expand Down
27 changes: 27 additions & 0 deletions config/validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"regexp"

"github.com/go-playground/validator/v10"
)

var (
lowercaseIDRegexp = regexp.MustCompile("^[a-z0-9]([a-z0-9_-]*[a-z0-9]+|)$")
)

const (
lowercaseID = "lowercase_id"
)

func RegisterAllValidators(v *validator.Validate) error {
return v.RegisterValidation(lowercaseID, LowercaseID)
}

// LowercaseID is the validation function for validating if the current field
// is not empty and contains only lowercase chars, numbers, hyphens or underscores.
// It's similar to RFC 1035 DNS label but not the same.
func LowercaseID(fl validator.FieldLevel) bool {
valAsString := fl.Field().String()
return lowercaseIDRegexp.MatchString(valAsString)
}
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This example uses 3D tiles of New York.
- Start GoKoala as specified in the root [README](../README.md#run)
and provide `config_3d.yaml` as the config file.
- Open http://localhost:8080 to explore the landing page
- Call http://localhost:8080/collections/NewYork/3dtiles/6/0/1.b3dm to download a specific 3D tile
- Call http://localhost:8080/collections/newyork/3dtiles/6/0/1.b3dm to download a specific 3D tile

## OGC API All/Complete example

Expand Down
2 changes: 1 addition & 1 deletion examples/config_3d.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ogcApi:
3dgeovolumes:
tileServer: https://maps.ecere.com/3DAPI/collections/
collections:
- id: NewYork
- id: newyork
# optional basepath to 3D tiles on the tileserver. Defaults to the collection ID.
tileServerPath: "NewYork/3DTiles"
# URI template for individual 3D tiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ ogcApi:
3dgeovolumes:
tileServer: https://example.com
collections:
- id: B
- id: Z
- id: C
- id: b
- id: z
- id: c
tiles:
collections:
- id: Z
- id: z
tileServer: https://example.com
- id: A
- id: a
tileServer: https://example.com
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ ogcApi:
3dgeovolumes:
tileServer: https://example.com
collections:
- id: B
- id: b
metadata:
title: Bear
- id: Z
- id: z
metadata:
title: Chicken
- id: C
- id: c
metadata:
title: Bird
tiles:
collections:
- id: Z
- id: z
tileServer: https://example.com
metadata:
title: Chicken
- id: A
- id: a
tileServer: https://example.com
metadata:
title: Horse
18 changes: 9 additions & 9 deletions internal/engine/testdata/config_collections_order_literal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ license:
url: https://creativecommons.org/publicdomain/zero/1.0/deed.nl
baseUrl: http://localhost:8181
collectionOrder:
- Z
- C
- A
- B
- z
- c
- a
- b
ogcApi:
3dgeovolumes:
tileServer: https://example.com
collections:
- id: B
- id: Z
- id: C
- id: b
- id: z
- id: c
tiles:
collections:
- id: Z
- id: z
tileServer: https://example.com
- id: A
- id: a
tileServer: https://example.com
6 changes: 3 additions & 3 deletions internal/engine/testdata/config_collections_unique.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ ogcApi:
3dgeovolumes:
tileServer: https://example.com
collections:
- id: FooCollection
- id: foo_collection
tiles:
collections:
- id: BarCollection
- id: bar_collection
tileServer: https://example.com
- id: FooCollection
- id: foo_collection
tileServer: https://example.com
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ogcApi:
3dgeovolumes:
tileServer: https://maps.ecere.com/3DAPI/collections/
collections:
- id: NewYork
- id: newyork
# reference to common metadata
metadata: *collectionMetadata
tileServerPath: "NewYork/3DTiles"
Expand All @@ -48,7 +48,7 @@ ogcApi:
local:
file: ./examples/resources/addresses-rd.gpkg
collections:
- id: NewYork
- id: newyork
tableName: addresses
# reference to common metadata
metadata: *collectionMetadata
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2 class="card-header h5">
</h2>
<div class="card-body">
<ul>
<li>Ga naar de <a href="http://localhost:8180/collections/NewYork/3dtiles" aria-label="Ga naar de 3D Tiles">3D Tiles</a></li>
<li>Ga naar de <a href="http://localhost:8180/collections/newyork/3dtiles" aria-label="Ga naar de 3D Tiles">3D Tiles</a></li>
</ul>
</div>
</div>
Expand All @@ -14,11 +14,11 @@ <h2 class="card-header h5">
<div class="col-md-4 col-sm-12">
<div class="card h-100">
<h2 class="card-header h5">
<a href="http://localhost:8180/collections/NewYork/items" aria-label="Blader door de Features">Features</a>
<a href="http://localhost:8180/collections/newyork/items" aria-label="Blader door de Features">Features</a>
</h2>
<div class="card-body">
<p>
Blader door de <a href="http://localhost:8180/collections/NewYork/items" aria-label="Blader door de Features">Features</a> of ga direct naar de features in:
Blader door de <a href="http://localhost:8180/collections/newyork/items" aria-label="Blader door de Features">Features</a> of ga direct naar de features in:
</p>
<p>
<ul>
Expand All @@ -28,15 +28,15 @@ <h2 class="card-header h5">
<option value="http://www.opengis.net/def/crs/EPSG/0/28992">EPSG:28992</option>
</select>
als
<a href="http://localhost:8180/collections/NewYork" onclick="gotoFeatures('json', 'selectjson'); return false;">GeoJSON</a>.
<a href="http://localhost:8180/collections/newyork" onclick="gotoFeatures('json', 'selectjson'); return false;">GeoJSON</a>.
</li>
<li> <label for="selectjsonfg" class="visually-hidden">Ga naar Features in</label>
<select id="selectjsonfg">
<option value="https://www.opengis.net/def/crs/OGC/1.3/CRS84">WGS84</option>
<option value="http://www.opengis.net/def/crs/EPSG/0/28992">EPSG:28992</option>
</select>
als
<a href="http://localhost:8180/collections/NewYork" onclick="gotoFeatures('jsonfg', 'selectjsonfg'); return false;">JSON-FG</a>.
<a href="http://localhost:8180/collections/newyork" onclick="gotoFeatures('jsonfg', 'selectjsonfg'); return false;">JSON-FG</a>.
</li>
</ul>
</p>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "NewYork",
"title": "NewYork",
"id": "newyork",
"title": "newyork",
"description": "This is a description about the NewYork collection in Markdown. We offer both 3D Tiles and Features for this collection.",
"keywords": [
{
Expand Down Expand Up @@ -44,52 +44,52 @@
"type": "application/json",
"title": "This document as JSON",
"updated": "2023-05-10T12:00:00Z",
"href": "http://localhost:8180/collections/NewYork?f=json"
"href": "http://localhost:8180/collections/newyork?f=json"
},
{
"rel": "alternate",
"type": "text/html",
"title": "This document as HTML",
"updated": "2023-05-10T12:00:00Z",
"href": "http://localhost:8180/collections/NewYork?f=html"
"href": "http://localhost:8180/collections/newyork?f=html"
},
{
"rel": "preview",
"type": "image/png",
"title": "Thumbnail for NewYork",
"title": "Thumbnail for newyork",
"href": "http://localhost:8180/resources/3d.png"
},
{
"rel": "items",
"type": "application/json+3dtiles",
"title": "Tileset definition of collection NewYork according to the OGC 3D Tiles specification",
"href": "http://localhost:8180/collections/NewYork/3dtiles?f=json"
"title": "Tileset definition of collection newyork according to the OGC 3D Tiles specification",
"href": "http://localhost:8180/collections/newyork/3dtiles?f=json"
},
{
"rel": "items",
"type": "application/geo+json",
"title": "The JSON representation of the NewYork features served from this endpoint",
"href": "http://localhost:8180/collections/NewYork/items?f=json"
"title": "The JSON representation of the newyork features served from this endpoint",
"href": "http://localhost:8180/collections/newyork/items?f=json"
},
{
"rel": "items",
"type": "application/vnd.ogc.fg+json",
"title": "The JSON-FG representation of the NewYork features served from this endpoint",
"href": "http://localhost:8180/collections/NewYork/items?f=jsonfg"
"title": "The JSON-FG representation of the newyork features served from this endpoint",
"href": "http://localhost:8180/collections/newyork/items?f=jsonfg"
},
{
"rel": "items",
"type": "text/html",
"title": "The HTML representation of the NewYork features served from this endpoint",
"href": "http://localhost:8180/collections/NewYork/items?f=html"
"title": "The HTML representation of the newyork features served from this endpoint",
"href": "http://localhost:8180/collections/newyork/items?f=html"
}
],
"content": [
{
"rel": "original",
"type": "application/json+3dtiles",
"title": "Tileset definition of collection NewYork according to the OGC 3D Tiles specification",
"href": "http://localhost:8180/collections/NewYork/3dtiles?f=json",
"title": "Tileset definition of collection newyork according to the OGC 3D Tiles specification",
"href": "http://localhost:8180/collections/newyork/3dtiles?f=json",
"collectionType": "3d-container"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"isPartOf": "http:\/\/localhost:8180?f=html",
"name": "New York - NewYork",
"description": "This is a description about the NewYork collection in Markdown. We offer both 3D Tiles and Features for this collection.",
"url": "http:\/\/localhost:8180/collections/NewYork?f=html","keywords": ["Keyword1","Keyword2"],"license": "https:\/\/creativecommons.org\/publicdomain\/zero\/1.0\/deed.nl",
"url": "http:\/\/localhost:8180/collections/newyork?f=html","keywords": ["Keyword1","Keyword2"],"license": "https:\/\/creativecommons.org\/publicdomain\/zero\/1.0\/deed.nl",
"isAccessibleForFree": true
,"thumbnailUrl": "http:\/\/localhost:8180/resources/3d.png"
,"version": "2023-05-10"
Expand Down

0 comments on commit 0ce5211

Please sign in to comment.