Newer
Older
/*
* Copyright (c) 2020 InterDigital Communications, 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.
*
* AdvantEDGE Platform Controller REST API
*
* This API is the main Platform Controller API for scenario configuration & sandbox management <p>**Micro-service**<br>[meep-pfm-ctrl](https://github.com/InterDigitalInc/AdvantEDGE/tree/master/go-apps/meep-platform-ctrl) <p>**Type & Usage**<br>Platform main interface used by controller software to configure scenarios and manage sandboxes in the AdvantEDGE platform <p>**Details**<br>API details available at _your-AdvantEDGE-ip-address/api_
*
* API version: 1.0.0
* Contact: AdvantEDGE@InterDigital.com
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package server
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Kevin Di Lallo
committed
"net/url"
"sync"
dataModel "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-data-model"
met "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics"
Kevin Di Lallo
committed
mq "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-mq"
pcc "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-platform-ctrl-client"
sm "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-sessions"
users "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-users"
"github.com/google/go-github/github"
Kevin Di Lallo
committed
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
const OAUTH_PROVIDER_GITHUB = "github"
const OAUTH_PROVIDER_GITLAB = "gitlab"
Kevin Di Lallo
committed
const OAUTH_PROVIDER_LOCAL = "local"
Kevin Di Lallo
committed
const serviceName = "Auth Service"
Kevin Di Lallo
committed
const moduleName = "meep-auth-svc"
const moduleNamespace = "default"
const postgisUser = "postgres"
const postgisPwd = "pwd"
const pfmCtrlBasepath = "http://meep-platform-ctrl/platform-ctrl/v1"
Kevin Di Lallo
committed
Kevin Di Lallo
committed
// Permission Configuration types
type Permission struct {
Mode string `yaml:"mode"`
Roles map[string]string `yaml:"roles"`
}
type Fileserver struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Sbox bool `yaml:"sbox"`
Mode string `yaml:"mode"`
Roles map[string]string `yaml:"roles"`
}
type Endpoint struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Method string `yaml:"method"`
Sbox bool `yaml:"sbox"`
Mode string `yaml:"mode"`
Roles map[string]string `yaml:"roles"`
}
type Service struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Sbox bool `yaml:"sbox"`
Default Permission `yaml:"default"`
Endpoints []Endpoint `yaml:"endpoints"`
Kevin Di Lallo
committed
}
type PermissionsConfig struct {
Default Permission `yaml:"default"`
Fileservers []Fileserver `yaml:"fileservers"`
Services []Service `yaml:"services"`
Kevin Di Lallo
committed
}
// Auth Service types
type AuthRoute struct {
Name string
Method string
Pattern string
Prefix bool
}
Kevin Di Lallo
committed
type LoginRequest struct {
provider string
createSandbox string
timer *time.Timer
Kevin Di Lallo
committed
}
Kevin Di Lallo
committed
type PermissionsCache struct {
Default *Permission
Fileservers map[string]*Permission
Services map[string]map[string]*Permission
}
Kevin Di Lallo
committed
type AuthSvc struct {
sessionMgr *sm.SessionMgr
userStore *users.Connector
metricStore *met.MetricStore
Kevin Di Lallo
committed
mqGlobal *mq.MsgQueue
pfmCtrlClient *pcc.APIClient
maxSessions int
uri string
oauthConfigs map[string]*oauth2.Config
loginRequests map[string]*LoginRequest
Kevin Di Lallo
committed
router *mux.Router
cache PermissionsCache
Kevin Di Lallo
committed
}
var mutex sync.Mutex
var gitlabApiUrl = ""
Kevin Di Lallo
committed
// Declare as variables to enable overwrite in test
var redisDBAddr = "meep-redis-master:6379"
var influxDBAddr string = "http://meep-influxdb.default.svc.cluster.local:8086"
Kevin Di Lallo
committed
// Auth Service
var authSvc *AuthSvc
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Metrics
var (
metricSessionLogin = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "auth_svc_session_login_total",
Help: "The total number of session login attempts",
}, []string{"type"})
metricSessionLogout = promauto.NewCounter(prometheus.CounterOpts{
Name: "auth_svc_session_logout_total",
Help: "The total number of session logout attempts",
})
metricSessionSuccess = promauto.NewCounter(prometheus.CounterOpts{
Name: "auth_svc_session_success_total",
Help: "The total number of successful sessions",
})
metricSessionFail = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "auth_svc_session_fail_total",
Help: "The total number of failed session login attempts",
}, []string{"type"})
metricSessionTimeout = promauto.NewCounter(prometheus.CounterOpts{
Name: "auth_svc_session_timeout_total",
Help: "The total number of timed out sessions",
})
metricSessionActive = promauto.NewGauge(prometheus.GaugeOpts{
Name: "auth_svc_session_active",
Help: "The number of active sessions",
})
metricSessionDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "auth_svc_session_duration",
Help: "A histogram of session durations",
Buckets: prometheus.LinearBuckets(20, 20, 6),
Kevin Di Lallo
committed
func Init() (err error) {
// Create new Platform Controller
authSvc = new(AuthSvc)
// Create message queue
authSvc.mqGlobal, err = mq.NewMsgQueue(mq.GetGlobalName(), moduleName, moduleNamespace, redisDBAddr)
if err != nil {
log.Error("Failed to create Message Queue with error: ", err)
return err
}
log.Info("Message Queue created")
// Create Platform Controller REST API client
pfmCtrlClientCfg := pcc.NewConfiguration()
pfmCtrlClientCfg.BasePath = pfmCtrlBasepath
authSvc.pfmCtrlClient = pcc.NewAPIClient(pfmCtrlClientCfg)
if authSvc.pfmCtrlClient == nil {
err := errors.New("Failed to create Platform Ctrl REST API client")
return err
Loading
Loading full blame…