feat(protocol): add gateway provider session work
This commit is contained in:
+244
-1
@@ -12,7 +12,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const SchemaSHA256 = "6b8631bf2b2aa12b14d0bc4d136af39a632e85b3237dc5614469ba09d93f5fca"
|
||||
const SchemaSHA256 = "c075c6fc472ce858337c3ad6c4b2f4dcfa88446d5471afb46c2f6931cbcf1b1d"
|
||||
const ProtocolVersion = "1.0.0"
|
||||
const CurrentWireVersion = "1"
|
||||
const NMinus1WireVersion = "0"
|
||||
@@ -240,6 +240,25 @@ type PageInfo struct {
|
||||
NextCursor string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type ProviderSessionWork struct {
|
||||
Version string `json:"version"`
|
||||
SessionID string `json:"session_id"`
|
||||
GatewayID string `json:"gateway_id"`
|
||||
ReconnectSequence int64 `json:"reconnect_sequence"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
ProviderProfile string `json:"provider_profile"`
|
||||
ProviderIdentity string `json:"provider_identity"`
|
||||
PolicyVersionID string `json:"policy_version_id"`
|
||||
ApplicationID string `json:"application_id"`
|
||||
ManagementHost string `json:"management_host"`
|
||||
ManagementPort int64 `json:"management_port"`
|
||||
StreamHost string `json:"stream_host"`
|
||||
StreamPort int64 `json:"stream_port"`
|
||||
ClientCertificatePem string `json:"client_certificate_pem"`
|
||||
ClientPrivateKeyPem string `json:"client_private_key_pem"`
|
||||
ServerCertificatePem string `json:"server_certificate_pem"`
|
||||
}
|
||||
|
||||
type ProviderState struct {
|
||||
Version string `json:"version"`
|
||||
SessionID string `json:"session_id"`
|
||||
@@ -2917,6 +2936,230 @@ func EncodePageInfo(value PageInfo) ([]byte, error) {
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
func (v ProviderSessionWork) Validate() error {
|
||||
var violations []FieldViolation
|
||||
if v.Version == "" {
|
||||
violations = append(violations, FieldViolation{Field: "version", Code: "required"})
|
||||
}
|
||||
if v.Version != "1" && v.Version != "" {
|
||||
violations = append(violations, FieldViolation{Field: "version", Code: "invalid_value"})
|
||||
}
|
||||
if v.SessionID == "" {
|
||||
violations = append(violations, FieldViolation{Field: "session_id", Code: "required"})
|
||||
}
|
||||
if len(v.SessionID) < 1 && v.SessionID != "" {
|
||||
violations = append(violations, FieldViolation{Field: "session_id", Code: "min_length"})
|
||||
}
|
||||
if len(v.SessionID) > 128 {
|
||||
violations = append(violations, FieldViolation{Field: "session_id", Code: "max_length"})
|
||||
}
|
||||
if v.GatewayID == "" {
|
||||
violations = append(violations, FieldViolation{Field: "gateway_id", Code: "required"})
|
||||
}
|
||||
if len(v.GatewayID) < 1 && v.GatewayID != "" {
|
||||
violations = append(violations, FieldViolation{Field: "gateway_id", Code: "min_length"})
|
||||
}
|
||||
if len(v.GatewayID) > 128 {
|
||||
violations = append(violations, FieldViolation{Field: "gateway_id", Code: "max_length"})
|
||||
}
|
||||
if v.ReconnectSequence != 0 && v.ReconnectSequence < 0 {
|
||||
violations = append(violations, FieldViolation{Field: "reconnect_sequence", Code: "minimum"})
|
||||
}
|
||||
if v.ExpiresAt == "" {
|
||||
violations = append(violations, FieldViolation{Field: "expires_at", Code: "required"})
|
||||
}
|
||||
if len(v.ExpiresAt) > 64 {
|
||||
violations = append(violations, FieldViolation{Field: "expires_at", Code: "max_length"})
|
||||
}
|
||||
if v.ExpiresAt != "" {
|
||||
if parsed, err := time.Parse(time.RFC3339Nano, v.ExpiresAt); err != nil || parsed.UTC().Format(time.RFC3339Nano) != v.ExpiresAt {
|
||||
violations = append(violations, FieldViolation{Field: "expires_at", Code: "invalid_time"})
|
||||
}
|
||||
}
|
||||
if v.ProviderProfile == "" {
|
||||
violations = append(violations, FieldViolation{Field: "provider_profile", Code: "required"})
|
||||
}
|
||||
if v.ProviderProfile != "apollo" && v.ProviderProfile != "" {
|
||||
violations = append(violations, FieldViolation{Field: "provider_profile", Code: "invalid_value"})
|
||||
}
|
||||
if v.ProviderIdentity == "" {
|
||||
violations = append(violations, FieldViolation{Field: "provider_identity", Code: "required"})
|
||||
}
|
||||
if len(v.ProviderIdentity) < 1 && v.ProviderIdentity != "" {
|
||||
violations = append(violations, FieldViolation{Field: "provider_identity", Code: "min_length"})
|
||||
}
|
||||
if len(v.ProviderIdentity) > 256 {
|
||||
violations = append(violations, FieldViolation{Field: "provider_identity", Code: "max_length"})
|
||||
}
|
||||
if v.PolicyVersionID == "" {
|
||||
violations = append(violations, FieldViolation{Field: "policy_version_id", Code: "required"})
|
||||
}
|
||||
if len(v.PolicyVersionID) < 1 && v.PolicyVersionID != "" {
|
||||
violations = append(violations, FieldViolation{Field: "policy_version_id", Code: "min_length"})
|
||||
}
|
||||
if len(v.PolicyVersionID) > 128 {
|
||||
violations = append(violations, FieldViolation{Field: "policy_version_id", Code: "max_length"})
|
||||
}
|
||||
if v.ApplicationID == "" {
|
||||
violations = append(violations, FieldViolation{Field: "application_id", Code: "required"})
|
||||
}
|
||||
if len(v.ApplicationID) < 1 && v.ApplicationID != "" {
|
||||
violations = append(violations, FieldViolation{Field: "application_id", Code: "min_length"})
|
||||
}
|
||||
if len(v.ApplicationID) > 128 {
|
||||
violations = append(violations, FieldViolation{Field: "application_id", Code: "max_length"})
|
||||
}
|
||||
if v.ManagementHost == "" {
|
||||
violations = append(violations, FieldViolation{Field: "management_host", Code: "required"})
|
||||
}
|
||||
if len(v.ManagementHost) < 1 && v.ManagementHost != "" {
|
||||
violations = append(violations, FieldViolation{Field: "management_host", Code: "min_length"})
|
||||
}
|
||||
if len(v.ManagementHost) > 256 {
|
||||
violations = append(violations, FieldViolation{Field: "management_host", Code: "max_length"})
|
||||
}
|
||||
if v.ManagementPort == 0 {
|
||||
violations = append(violations, FieldViolation{Field: "management_port", Code: "required"})
|
||||
}
|
||||
if v.ManagementPort != 0 && v.ManagementPort < 1 {
|
||||
violations = append(violations, FieldViolation{Field: "management_port", Code: "minimum"})
|
||||
}
|
||||
if v.ManagementPort > 65535 {
|
||||
violations = append(violations, FieldViolation{Field: "management_port", Code: "maximum"})
|
||||
}
|
||||
if v.StreamHost == "" {
|
||||
violations = append(violations, FieldViolation{Field: "stream_host", Code: "required"})
|
||||
}
|
||||
if len(v.StreamHost) < 1 && v.StreamHost != "" {
|
||||
violations = append(violations, FieldViolation{Field: "stream_host", Code: "min_length"})
|
||||
}
|
||||
if len(v.StreamHost) > 256 {
|
||||
violations = append(violations, FieldViolation{Field: "stream_host", Code: "max_length"})
|
||||
}
|
||||
if v.StreamPort == 0 {
|
||||
violations = append(violations, FieldViolation{Field: "stream_port", Code: "required"})
|
||||
}
|
||||
if v.StreamPort != 0 && v.StreamPort < 1 {
|
||||
violations = append(violations, FieldViolation{Field: "stream_port", Code: "minimum"})
|
||||
}
|
||||
if v.StreamPort > 65535 {
|
||||
violations = append(violations, FieldViolation{Field: "stream_port", Code: "maximum"})
|
||||
}
|
||||
if v.ClientCertificatePem == "" {
|
||||
violations = append(violations, FieldViolation{Field: "client_certificate_pem", Code: "required"})
|
||||
}
|
||||
if len(v.ClientCertificatePem) < 1 && v.ClientCertificatePem != "" {
|
||||
violations = append(violations, FieldViolation{Field: "client_certificate_pem", Code: "min_length"})
|
||||
}
|
||||
if len(v.ClientCertificatePem) > 32768 {
|
||||
violations = append(violations, FieldViolation{Field: "client_certificate_pem", Code: "max_length"})
|
||||
}
|
||||
if v.ClientPrivateKeyPem == "" {
|
||||
violations = append(violations, FieldViolation{Field: "client_private_key_pem", Code: "required"})
|
||||
}
|
||||
if len(v.ClientPrivateKeyPem) < 1 && v.ClientPrivateKeyPem != "" {
|
||||
violations = append(violations, FieldViolation{Field: "client_private_key_pem", Code: "min_length"})
|
||||
}
|
||||
if len(v.ClientPrivateKeyPem) > 32768 {
|
||||
violations = append(violations, FieldViolation{Field: "client_private_key_pem", Code: "max_length"})
|
||||
}
|
||||
if v.ServerCertificatePem == "" {
|
||||
violations = append(violations, FieldViolation{Field: "server_certificate_pem", Code: "required"})
|
||||
}
|
||||
if len(v.ServerCertificatePem) < 1 && v.ServerCertificatePem != "" {
|
||||
violations = append(violations, FieldViolation{Field: "server_certificate_pem", Code: "min_length"})
|
||||
}
|
||||
if len(v.ServerCertificatePem) > 32768 {
|
||||
violations = append(violations, FieldViolation{Field: "server_certificate_pem", Code: "max_length"})
|
||||
}
|
||||
if len(violations) > 0 {
|
||||
return ValidationError{Violations: violations}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DecodeProviderSessionWork(data []byte) (ProviderSessionWork, error) {
|
||||
var value ProviderSessionWork
|
||||
if len(data) > 1024*1024 {
|
||||
return value, errors.New("protocol payload exceeds limit")
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
return value, err
|
||||
}
|
||||
if raw, ok := fields["application_id"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "application_id", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["client_certificate_pem"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "client_certificate_pem", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["client_private_key_pem"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "client_private_key_pem", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["expires_at"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "expires_at", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["gateway_id"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "gateway_id", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["management_host"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "management_host", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["management_port"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "management_port", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["policy_version_id"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "policy_version_id", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["provider_identity"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "provider_identity", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["provider_profile"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "provider_profile", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["reconnect_sequence"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "reconnect_sequence", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["server_certificate_pem"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "server_certificate_pem", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["session_id"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "session_id", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["stream_host"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "stream_host", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["stream_port"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "stream_port", Code: "required"}}}
|
||||
}
|
||||
if raw, ok := fields["version"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return value, ValidationError{Violations: []FieldViolation{{Field: "version", Code: "required"}}}
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return value, err
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); err != io.EOF {
|
||||
if err == nil {
|
||||
return value, errors.New("trailing JSON value")
|
||||
}
|
||||
return value, err
|
||||
}
|
||||
if err := value.Validate(); err != nil {
|
||||
return value, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func EncodeProviderSessionWork(value ProviderSessionWork) ([]byte, error) {
|
||||
if err := value.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
func (v ProviderState) Validate() error {
|
||||
var violations []FieldViolation
|
||||
if v.Version == "" {
|
||||
|
||||
Reference in New Issue
Block a user