-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
168 lines (147 loc) · 6.64 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package api
import (
"crypto/x509"
"fmt"
"net/http"
"time"
"github.com/akyoto/cache"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"k8s.io/client-go/kubernetes"
)
type APIHandler struct {
Server *gin.Engine
DB *gorm.DB
clientset kubernetes.Interface
ssoConfig *SSOConfig
serviceIP *string
tenant string
Cache *cache.Cache
dashboard *string
fswatchImage string
}
type SSOConfig struct {
URL string
saml *SAMLOptions
}
type SAMLOptions struct {
issuer string
crt *x509.Certificate
recipient string
}
func NewSAMLConfig(issuer, recipient, metadataURL string) (*SSOConfig, error) {
if issuer == "" || recipient == "" || metadataURL == "" {
return nil, nil
}
crt, err := fetchIDPCertificate(metadataURL)
if err != nil {
return nil, err
}
if crt == nil {
return nil, fmt.Errorf("could not get certification from metadata url")
}
return &SSOConfig{
saml: &SAMLOptions{
issuer: issuer,
recipient: recipient,
crt: crt,
},
}, nil
}
func NewAPIHandler(db *gorm.DB, clientset kubernetes.Interface, ssoConfig *SSOConfig, serviceIP, dashboard *string, fswatchImage string) *APIHandler {
return &APIHandler{
Server: gin.Default(),
DB: db,
clientset: clientset,
ssoConfig: ssoConfig,
serviceIP: serviceIP,
Cache: cache.New(20 * time.Second),
dashboard: dashboard,
fswatchImage: fswatchImage,
}
}
func (h APIHandler) RegisterRoutes() {
h.Server.Use(h.corsOK)
preauth := h.Server.Group("/")
preauth.GET("/noauthtest", func(c *gin.Context) {
c.JSON(200, response(200, "", []string{"Please come again!"}))
})
preauth.POST("/noauthtest", func(c *gin.Context) {
var data any
err := c.BindJSON(&data)
if err != nil {
c.AbortWithError(http.StatusNotAcceptable, err)
return
}
c.JSON(200, response(200, "", []any{data}))
})
preauth.POST("/login", h.login)
preauth.POST("/refresh", h.loginWithRefreshToken)
preauth.GET("/connect", h.defaultConnectMethod) // Determine preferred auth method
preauth.GET("/sso", h.ssoRedirecter)
preauth.POST("/sso/saml", h.samlConnecter)
basic := h.Server.Group("/")
basic.Use(validateJwt)
basic.GET("/dashboard", h.dashboardRedirect)
authenticatedAPIV1 := h.Server.Group("/api/v1/")
authenticatedAPIV1.Use(validateJwt)
authenticatedAPIV1.GET("/", h.Index)
authenticatedAPIV1.GET("/workflows", h.workflows)
cluster := authenticatedAPIV1.Group("/cluster")
cluster.POST("/", h.AddCluster) // Resource from Add/Update/Delete event
cluster.GET("/:cluster_name/health", h.VClusterHealth)
cluster.GET("/:cluster_name/tfohealth", h.VClusterTFOHealth)
cluster.PUT("/:cluster_name/sync-dependencies", h.SyncEvent)
cluster.POST("/:cluster_name/event", h.ResourceEvent) // routes.GET("/cluster-name/:cluster_name", h.GetCluster) // to be removed
cluster.PUT("/:cluster_name/event", h.ResourceEvent)
cluster.DELETE("/:cluster_name/event/:tfo_resource_uuid", h.ResourceEvent)
cluster.GET("/:cluster_name/resource/:namespace/:name/poll", h.ResourcePoll) // Poll for resource objects in the cluster
cluster.GET("/:cluster_name/resource/:namespace/:name/debug", h.Debugger)
cluster.GET("/:cluster_name/debug/:namespace/:name", h.Debugger) // Alias
cluster.GET("/:cluster_name/resource/:namespace/:name/unlock", h.UnlockTerraform)
cluster.GET("/:cluster_name/resource/:namespace/:name/status", h.ResourceStatusCheck)
cluster.GET("/:cluster_name/status/:namespace/:name", h.ResourceStatusCheck) // Alias
cluster.GET("/:cluster_name/resource/:namespace/:name/last-task-log", h.LastTaskLog)
cluster.PUT("/:cluster_name/resource/:namespace/:name/rerun", h.rerunWorkflow)
cluster.GET("/:cluster_name/resource/:namespace/:name/generation/:generation/info", h.getWorkflowInfo)
metrics := authenticatedAPIV1.Group("/metrics")
metrics.GET("/total/resources", h.TotalResources)
metrics.GET("/total/failed-resources", h.TotalFailedResources)
// DEPRECATED usage of clusterid is being removed. todo ensure galleybytes projects aren't using this
clusterid := authenticatedAPIV1.Group("/cluster-id")
clusterid.GET("/:cluster_id", h.GetCluster)
clusterid.GET("/:cluster_id/resources", h.GetClustersResources) // List Resources
// List Clusters
authenticatedAPIV1.GET("/clusters", h.ListClusters)
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid", h.GetResourceByUUID)
// List Generations
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generations", h.GetDistinctGeneration)
// ReourceSpec
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generation/:generation/resource-spec", h.getWorkflowResourceConfiguration)
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generation/:generation/tasks", h.getAllTasksGeneratedForResource)
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generation/:generation/latest-tasks", h.getHighestRerunOfTasksGeneratedForResource)
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generation/:generation/approval-status", h.getApprovalStatusForResource)
authenticatedAPIV1.POST("/resource/:tfo_resource_uuid/generation/:generation/approval", h.setApprovalForResource)
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generation/:generation/logs", h.preLogs)
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/generation/:generation/ws-logs", h.websocketLogs)
// authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/logs", h.GetClustersResourcesLogs)
// authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/logs/generation/:generation", h.GetClustersResourcesLogs)
// authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/logs/generation/:generation/task/:task_type", h.GetClustersResourcesLogs)
// authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/logs/generation/:generation/task/:task_type/rerun/:rerun", h.GetClustersResourcesLogs)
// authenticatedAPIV1.GET("/task/:task_pod_uuid/logs", h.GetTFOTaskLogsViaTask)
// authenticatedAPIV1.GET("/task/:task_pod_uuid", h.GetTaskPod) // TODO Should getting a task out of band (ie not with cluster info) be allowed?
// Tasks via task JWT
authenticatedTask := h.Server.Group("/api/v1/task")
authenticatedTask.Use(validateTaskJWT)
authenticatedTask.POST("", h.AddTaskPod)
authenticatedTask.GET("/status", h.ResourceStatusCheckViaTask)
authenticatedTask.POST("/status", h.UpdateResourceStatusViaTask)
authenticatedTask.GET("/:task_pod_uuid/approval-status", h.GetApprovalStatusViaTaskPodUUID)
// Approval
authenticatedAPIV1.GET("/resource/:tfo_resource_uuid/approval-status", h.GetApprovalStatus)
authenticatedAPIV1.POST("/approval/:task_pod_uuid", h.UpdateApproval)
authenticatedAPIV1.GET("/approvals", h.AllApprovals)
// Websockets will be prefixed with /ws
sockets := h.Server.Group("/ws/")
sockets.GET("/:tfo_resource_uuid", h.ResourceLogWatcher)
}