Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defines the Information Service on top of Kong's Root endpoint #65

Merged
merged 19 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Client struct {
ACLs AbstractACLService
Oauth2Credentials AbstractOauth2Service
Tags AbstractTagService
Information AbstractInformationService

logger io.Writer
debug bool
Expand Down Expand Up @@ -150,6 +151,8 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) {
kong.Oauth2Credentials = (*Oauth2Service)(&kong.common)
kong.Tags = (*TagService)(&kong.common)

kong.Information = (*InformationService)(&kong.common)

kong.CustomEntities = (*CustomEntityService)(&kong.common)
kong.Registry = custom.NewDefaultRegistry()

Expand Down
106 changes: 106 additions & 0 deletions kong/information_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package kong

import (
"context"
"errors"
"strings"
)

// AbstractInformationService handles Kong's information.
type AbstractInformationService interface {
// Version returns the Kong version
Version(ctx context.Context) (string, error)
// IsEnterprise check if Kong is enterprise edition
IsEnterprise(ctx context.Context) (bool, error)
// Database retrieves the database used by Kong
Database(ctx context.Context) (string, error)
// IsInMemory check if Kong is in memory
IsInMemory(ctx context.Context) (bool, error)
// IsRBACEnabled check if RBAC are enabled
IsRBACEnabled(ctx context.Context) (bool, error)
// IsPortalEnabled check if the portal is enabled
IsPortalEnabled(ctx context.Context) (bool, error)
}

// InformationService handles Kong's information.
type InformationService service

// Version returns the Kong version
func (s *InformationService) Version(ctx context.Context) (string, error) {
information, err := s.client.Root(ctx)
if err != nil {
return "", err
}
return VersionFromInfo(information), nil

}

// IsEnterprise check if Kong is enterprise edition
func (s *InformationService) IsEnterprise(ctx context.Context) (bool, error) {
version, err := s.Version(ctx)
if err != nil {
return false, err
}
return strings.Contains(version, "enterprise"), nil

}

// Database retrieves the database used by Kong
func (s *InformationService) Database(ctx context.Context) (string, error) {
information, err := s.client.Root(ctx)
if err != nil {
return "", err
}
configuration := configurationFromInfo(information)
database, ok := configuration["database"]
if !ok {
return "", errors.New("database could not be parsed from kong configuration")
}
return database.(string), nil

}

// IsInMemory check if Kong is in memory
func (s *InformationService) IsInMemory(ctx context.Context) (bool, error) {
database, err := s.Database(ctx)
if err != nil {
return false, err
}
return "off" == database, nil
}

// IsRBACEnabled check if RBAC are enabled
func (s *InformationService) IsRBACEnabled(ctx context.Context) (bool, error) {
information, err := s.client.Root(ctx)
if err != nil {
return false, err
}
configuration := configurationFromInfo(information)
rbac, ok := configuration["rbac"]
if !ok {
return false, errors.New("rbac could not be parsed from kong configuration")
}
return "on" == rbac.(string), nil
}

// IsPortalEnabled check if the portal is enabled
func (s *InformationService) IsPortalEnabled(ctx context.Context) (bool, error) {
information, err := s.client.Root(ctx)
if err != nil {
return false, err
}
configuration := configurationFromInfo(information)
portal, ok := configuration["portal"]
if !ok {
return false, errors.New("portal could not be parsed from kong configuration")
}
return portal.(bool), nil
}

func configurationFromInfo(info map[string]interface{}) map[string]interface{} {
configuration, ok := info["configuration"]
if !ok {
return make(map[string]interface{})
}
return configuration.(map[string]interface{})
}