mirror of
https://github.com/gravitational/teleport
synced 2024-10-21 17:53:28 +00:00
161 lines
4.5 KiB
Go
161 lines
4.5 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 (
|
|
"context"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/lib/utils"
|
|
)
|
|
|
|
// AppGetter defines interface for fetching application resources.
|
|
type AppGetter interface {
|
|
// GetApps returns all application resources.
|
|
GetApps(context.Context) ([]types.Application, error)
|
|
// GetApp returns the specified application resource.
|
|
GetApp(ctx context.Context, name string) (types.Application, error)
|
|
}
|
|
|
|
// Apps defines an interface for managing application resources.
|
|
type Apps interface {
|
|
// AppGetter provides methods for fetching application resources.
|
|
AppGetter
|
|
// CreateApp creates a new application resource.
|
|
CreateApp(context.Context, types.Application) error
|
|
// UpdateApp updates an existing application resource.
|
|
UpdateApp(context.Context, types.Application) error
|
|
// DeleteApp removes the specified application resource.
|
|
DeleteApp(ctx context.Context, name string) error
|
|
// DeleteAllApps removes all database resources.
|
|
DeleteAllApps(context.Context) error
|
|
}
|
|
|
|
// MarshalApp marshals Application resource to JSON.
|
|
func MarshalApp(app types.Application, opts ...MarshalOption) ([]byte, error) {
|
|
if err := app.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
switch app := app.(type) {
|
|
case *types.AppV3:
|
|
if !cfg.PreserveResourceID {
|
|
copy := *app
|
|
copy.SetResourceID(0)
|
|
app = ©
|
|
}
|
|
return utils.FastMarshal(app)
|
|
default:
|
|
return nil, trace.BadParameter("unsupported app resource %T", app)
|
|
}
|
|
}
|
|
|
|
// UnmarshalApp unmarshals Application resource from JSON.
|
|
func UnmarshalApp(data []byte, opts ...MarshalOption) (types.Application, error) {
|
|
if len(data) == 0 {
|
|
return nil, trace.BadParameter("missing app resource data")
|
|
}
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
var h types.ResourceHeader
|
|
if err := utils.FastUnmarshal(data, &h); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
switch h.Version {
|
|
case types.V3:
|
|
var app types.AppV3
|
|
if err := utils.FastUnmarshal(data, &app); err != nil {
|
|
return nil, trace.BadParameter(err.Error())
|
|
}
|
|
if err := app.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
if cfg.ID != 0 {
|
|
app.SetResourceID(cfg.ID)
|
|
}
|
|
if !cfg.Expires.IsZero() {
|
|
app.SetExpiry(cfg.Expires)
|
|
}
|
|
return &app, nil
|
|
}
|
|
return nil, trace.BadParameter("unsupported app resource version %q", h.Version)
|
|
}
|
|
|
|
// MarshalAppServer marshals the AppServer resource to JSON.
|
|
func MarshalAppServer(appServer types.AppServer, opts ...MarshalOption) ([]byte, error) {
|
|
if err := appServer.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
switch appServer := appServer.(type) {
|
|
case *types.AppServerV3:
|
|
if !cfg.PreserveResourceID {
|
|
copy := *appServer
|
|
copy.SetResourceID(0)
|
|
appServer = ©
|
|
}
|
|
return utils.FastMarshal(appServer)
|
|
default:
|
|
return nil, trace.BadParameter("unsupported app server resource %T", appServer)
|
|
}
|
|
}
|
|
|
|
// UnmarshalAppServer unmarshals AppServer resource from JSON.
|
|
func UnmarshalAppServer(data []byte, opts ...MarshalOption) (types.AppServer, error) {
|
|
if len(data) == 0 {
|
|
return nil, trace.BadParameter("missing app server data")
|
|
}
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
var h types.ResourceHeader
|
|
if err := utils.FastUnmarshal(data, &h); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
switch h.Version {
|
|
case types.V3:
|
|
var s types.AppServerV3
|
|
if err := utils.FastUnmarshal(data, &s); err != nil {
|
|
return nil, trace.BadParameter(err.Error())
|
|
}
|
|
if err := s.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
if cfg.ID != 0 {
|
|
s.SetResourceID(cfg.ID)
|
|
}
|
|
if !cfg.Expires.IsZero() {
|
|
s.SetExpiry(cfg.Expires)
|
|
}
|
|
return &s, nil
|
|
}
|
|
return nil, trace.BadParameter("unsupported app server resource version %q", h.Version)
|
|
}
|