Skip to content

Commit

Permalink
Use subtypes for YAML generated endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ikapelyukhin committed Nov 17, 2020
1 parent 3a00c84 commit 4d6daa4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/jetstream/plugins.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: private_github
auth_type: HttpBasic
- name: private_gitlab
auth_type: Bearer
- name: git
auth_types:
private_github: HttpBasic
private_gitlab: Bearer

49 changes: 31 additions & 18 deletions src/jetstream/plugins/yamlgenerated/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type GeneratedPlugin struct {
routePlugin func() (interfaces.RoutePlugin, error)
}

var authTypeToConnectTypeMap = map[string]string{
interfaces.AuthTypeHttpBasic: interfaces.AuthConnectTypeCreds,
interfaces.AuthTypeBearer: interfaces.AuthConnectTypeBearer,
}

func (gp GeneratedPlugin) Init() error { return gp.initMethod() }
func (gp GeneratedPlugin) GetMiddlewarePlugin() (interfaces.MiddlewarePlugin, error) {
return gp.middlewarePlugin()
Expand All @@ -35,7 +40,7 @@ func (gp GeneratedPlugin) GetRoutePlugin() (interfaces.RoutePlugin, error) {
type GeneratedEndpointPlugin struct {
portalProxy interfaces.PortalProxy
endpointType string
authType string
authTypes map[string]string
}

func (gep GeneratedEndpointPlugin) GetType() string {
Expand All @@ -57,16 +62,24 @@ func (gep GeneratedEndpointPlugin) Connect(ec echo.Context, cnsiRecord interface
return nil, false, err
}

connectType := params.ConnectType
authType, ok := gep.authTypes[cnsiRecord.SubType]
if !ok {
return nil, false, fmt.Errorf("Unknown subtype %q for endpoint type %q", cnsiRecord.SubType, gep.GetType())
}

expectedConnectType, ok := authTypeToConnectTypeMap[authType]
if !ok {
return nil, false, fmt.Errorf("Unknown authentication type %q for plugin %q", authType, gep.GetType())
}

if expectedConnectType != params.ConnectType {
return nil, false, fmt.Errorf("Only %q connect type is supported for %q.%q endpoints", expectedConnectType, gep.GetType(), cnsiRecord.SubType)
}

var tr *interfaces.TokenRecord

switch gep.authType {
switch params.ConnectType {
case interfaces.AuthConnectTypeCreds:
if connectType != interfaces.AuthTypeHttpBasic {
return nil, false, fmt.Errorf("Plugin %s supports only '%s' connect type", gep.GetType(), interfaces.AuthConnectTypeCreds)
}

if len(params.Username) == 0 || len(params.Password) == 0 {
return nil, false, errors.New("Need username and password")
}
Expand All @@ -80,19 +93,14 @@ func (gep GeneratedEndpointPlugin) Connect(ec echo.Context, cnsiRecord interface
RefreshToken: params.Username,
}
case interfaces.AuthConnectTypeBearer:
if connectType != interfaces.AuthTypeBearer {
return nil, false, fmt.Errorf("Plugin %s supports only '%s' connect type", gep.endpointType, interfaces.AuthConnectTypeCreds)
}

authString := ec.FormValue("token")
base64EncodedAuthString := base64.StdEncoding.EncodeToString([]byte(authString))

tr = &interfaces.TokenRecord{
AuthType: interfaces.AuthTypeBearer,
AuthToken: base64EncodedAuthString,
AuthType: interfaces.AuthTypeBearer,
AuthToken: base64EncodedAuthString,
RefreshToken: "token", // DB needs a non-empty value
}
default:
return nil, false, fmt.Errorf("Only '%s' authentication is supported for %s endpoints", gep.authType, gep.GetType())
}

return tr, false, nil
Expand Down Expand Up @@ -120,8 +128,8 @@ func (gep GeneratedEndpointPlugin) UpdateMetadata(info *interfaces.Info, userGUI
}

type PluginConfig struct {
Name string `yaml:"name"`
AuthType string `yaml:"auth_type"`
Name string `yaml:"name"`
AuthTypes map[string]string `yaml:"auth_types"`
}

func MakePluginsFromConfig() {
Expand All @@ -142,11 +150,16 @@ func MakePluginsFromConfig() {
}

for _, plugin := range config {
if len(plugin.Name) == 0 {
log.Errorf("Plugin must have a name")
return
}

log.Debugf("Generating plugin %s", plugin.Name)

gep := GeneratedEndpointPlugin{}
gep.endpointType = plugin.Name
gep.authType = plugin.AuthType
gep.authTypes = plugin.AuthTypes

gp := GeneratedPlugin{}
gp.initMethod = func() error { return nil }
Expand Down

0 comments on commit 4d6daa4

Please sign in to comment.