mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-11-13 02:17:25 +01:00
Add option for json validation
This commit is contained in:
parent
732801548f
commit
777f225e91
@ -20,6 +20,7 @@ var Keys schema.ProgramConfig = schema.ProgramConfig{
|
|||||||
DB: "./var/job.db",
|
DB: "./var/job.db",
|
||||||
Archive: json.RawMessage(`{\"kind\":\"file\",\"path\":\"./var/job-archive\"}`),
|
Archive: json.RawMessage(`{\"kind\":\"file\",\"path\":\"./var/job-archive\"}`),
|
||||||
DisableArchive: false,
|
DisableArchive: false,
|
||||||
|
Validate: false,
|
||||||
LdapConfig: nil,
|
LdapConfig: nil,
|
||||||
SessionMaxAge: "168h",
|
SessionMaxAge: "168h",
|
||||||
UiDefaults: map[string]interface{}{
|
UiDefaults: map[string]interface{}{
|
||||||
|
@ -6,6 +6,7 @@ package archive
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ClusterCockpit/cc-backend/internal/config"
|
||||||
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
||||||
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
||||||
)
|
)
|
||||||
@ -100,14 +102,17 @@ func (fsa *FsArchive) LoadJobMeta(job *schema.Job) (*schema.JobMeta, error) {
|
|||||||
|
|
||||||
func (fsa *FsArchive) LoadClusterCfg(name string) (*schema.Cluster, error) {
|
func (fsa *FsArchive) LoadClusterCfg(name string) (*schema.Cluster, error) {
|
||||||
|
|
||||||
f, err := os.Open(filepath.Join(fsa.path, name, "cluster.json"))
|
b, err := os.ReadFile(filepath.Join(fsa.path, name, "cluster.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("fsBackend LoadClusterCfg()- %v", err)
|
log.Errorf("fsBackend LoadClusterCfg()- %v", err)
|
||||||
return &schema.Cluster{}, err
|
return &schema.Cluster{}, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
if config.Keys.Validate {
|
||||||
|
if err := schema.Validate(schema.ClusterCfg, bytes.NewReader(b)); err != nil {
|
||||||
return DecodeCluster(bufio.NewReader(f))
|
return &schema.Cluster{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return DecodeCluster(bytes.NewReader(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fsa *FsArchive) Iter() <-chan *schema.JobMeta {
|
func (fsa *FsArchive) Iter() <-chan *schema.JobMeta {
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
||||||
|
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitEmptyPath(t *testing.T) {
|
func TestInitEmptyPath(t *testing.T) {
|
||||||
|
@ -78,6 +78,9 @@ type ProgramConfig struct {
|
|||||||
// do not write to the job-archive.
|
// do not write to the job-archive.
|
||||||
DisableArchive bool `json:"disable-archive"`
|
DisableArchive bool `json:"disable-archive"`
|
||||||
|
|
||||||
|
// Validate json input against schema
|
||||||
|
Validate bool `json:"validate"`
|
||||||
|
|
||||||
// For LDAP Authentication and user synchronisation.
|
// For LDAP Authentication and user synchronisation.
|
||||||
LdapConfig *LdapConfig `json:"ldap"`
|
LdapConfig *LdapConfig `json:"ldap"`
|
||||||
JwtConfig *JWTAuthConfig `json:"jwts"`
|
JwtConfig *JWTAuthConfig `json:"jwts"`
|
||||||
|
@ -5,10 +5,12 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/santhosh-tekuri/jsonschema"
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
||||||
|
"github.com/santhosh-tekuri/jsonschema/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Kind int
|
type Kind int
|
||||||
@ -19,7 +21,7 @@ const (
|
|||||||
ClusterCfg
|
ClusterCfg
|
||||||
)
|
)
|
||||||
|
|
||||||
func Validate(k Kind, v io.Reader) (err error) {
|
func Validate(k Kind, r io.Reader) (err error) {
|
||||||
var s *jsonschema.Schema
|
var s *jsonschema.Schema
|
||||||
|
|
||||||
switch k {
|
switch k {
|
||||||
@ -37,9 +39,15 @@ func Validate(k Kind, v io.Reader) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.Validate(v); err != nil {
|
var v interface{}
|
||||||
|
if err := json.NewDecoder(r).Decode(&v); err != nil {
|
||||||
|
log.Error("schema.Validate() - Failed to decode %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = s.Validate(v); err != nil {
|
||||||
|
return fmt.Errorf("%#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setup(t *testing.T) *api.RestApi {
|
func setup(t *testing.T) *api.RestApi {
|
||||||
|
Loading…
Reference in New Issue
Block a user