mirror of
https://github.com/gravitational/teleport
synced 2024-10-22 02:03:24 +00:00
cd2f4fceb7
* Remove JSON schema validation Removing JSON schema validation from all resource unmarshalers. --- what JSON schema gets us Looking at the JSON schema spec and our usage, here are the supposed benefits: - type validation - make sure incoming data uses the right types for the right fields - required fields - make sure that mandatory fields are set - defaulting - set defaults for fields - documentation - schema definition for our API objects Note that it does _not_ do: - fail on unknown fields in data - fail on a required field with an empty value --- what replaces it Based on the above, it may seem like JSON schema provides value. But it's not the case, let's break it down one by one: - type validation - unmarshaling JSON into a typed Go struct does this - required fields - only checks that the field was provided, doesn't actually check that a value is set (e.g. `"name": ""` will pass the `required` check) - so it's pretty useless for any real validation - and we already have a separate place for proper validation - `CheckAndSetDefaults` methods - defaulting - done in `CheckAndSetDefaults` methods - `Version` is the only annoying field, had to add it in a bunch of objects - documentation - protobuf definitions are the source of truth for our API schema --- the benefits - performance - schema validation does a few rounds of `json.Marshal/Unmarshal` in addition to actual validation; now we simply skip all that - maintenance - no need to keep protobuf and JSON schema definitions in sync anymore - creating new API objects - one error-prone step removed - (future) fewer dependencies - we can _almost_ remove the Go libraries for schema validation (one transient dependency keeping them around) * Remove services.SkipValidation No more JSON schema validation so this option is a noop.
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
/*
|
|
Copyright 2021 Gravitational, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package services
|
|
|
|
import (
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport/lib/defaults"
|
|
"github.com/gravitational/teleport/lib/utils"
|
|
)
|
|
|
|
// DefaultStaticTokens is used to get the default static tokens (empty list)
|
|
// when nothing is specified in file configuration.
|
|
func DefaultStaticTokens() StaticTokens {
|
|
return &StaticTokensV2{
|
|
Kind: KindStaticTokens,
|
|
Version: V2,
|
|
Metadata: Metadata{
|
|
Name: MetaNameStaticTokens,
|
|
Namespace: defaults.Namespace,
|
|
},
|
|
Spec: StaticTokensSpecV2{
|
|
StaticTokens: []ProvisionTokenV1{},
|
|
},
|
|
}
|
|
}
|
|
|
|
// UnmarshalStaticTokens unmarshals the StaticTokens resource from JSON.
|
|
func UnmarshalStaticTokens(bytes []byte, opts ...MarshalOption) (StaticTokens, error) {
|
|
var staticTokens StaticTokensV2
|
|
|
|
if len(bytes) == 0 {
|
|
return nil, trace.BadParameter("missing resource data")
|
|
}
|
|
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
if err := utils.FastUnmarshal(bytes, &staticTokens); err != nil {
|
|
return nil, trace.BadParameter(err.Error())
|
|
}
|
|
if err := staticTokens.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
if cfg.ID != 0 {
|
|
staticTokens.SetResourceID(cfg.ID)
|
|
}
|
|
if !cfg.Expires.IsZero() {
|
|
staticTokens.SetExpiry(cfg.Expires)
|
|
}
|
|
return &staticTokens, nil
|
|
}
|
|
|
|
// MarshalStaticTokens marshals the StaticTokens resource to JSON.
|
|
func MarshalStaticTokens(staticToken StaticTokens, opts ...MarshalOption) ([]byte, error) {
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
switch staticToken := staticToken.(type) {
|
|
case *StaticTokensV2:
|
|
if version := staticToken.GetVersion(); version != V2 {
|
|
return nil, trace.BadParameter("mismatched static token version %v and type %T", version, staticToken)
|
|
}
|
|
if !cfg.PreserveResourceID {
|
|
// avoid modifying the original object
|
|
// to prevent unexpected data races
|
|
copy := *staticToken
|
|
copy.SetResourceID(0)
|
|
staticToken = ©
|
|
}
|
|
return utils.FastMarshal(staticToken)
|
|
default:
|
|
return nil, trace.BadParameter("unrecognized static token version %T", staticToken)
|
|
}
|
|
}
|