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

add api that will generate a new token #30

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (h APIHandler) RegisterRoutes() {
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.PATCH("/:cluster_name/resource/:namespace/:name/token", h.manualTokenPatch)
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)
Expand Down
45 changes: 45 additions & 0 deletions pkg/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,51 @@ func (h APIHandler) updateResource(c *gin.Context) error {
return nil
}

// manualTokenPatch is used to re-submit a new token secret to the vcluster. Resources can generally
// use a refresh token, but this can be useful if the refresh token has been invalidated.
func (h APIHandler) manualTokenPatch(c *gin.Context) {
name := c.Param("name")
namespace := c.Param("namespace")
clusterName := c.Param("cluster_name")

var tfoResourceSpec models.TFOResourceSpec

result := h.DB.Raw(`
SELECT
tfo_resource_specs.*
FROM
tfo_resource_specs
JOIN
tfo_resources
ON tfo_resources.uuid = tfo_resource_specs.tfo_resource_uuid
JOIN
clusters
ON clusters.id = tfo_resources.cluster_id
WHERE clusters.name = ?
AND tfo_resources.namespace = ?
AND tfo_resources.name = ?
AND tfo_resource_specs.generation = tfo_resources.current_generation
`, clusterName, namespace, name).Scan(&tfoResourceSpec)
if result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, response(http.StatusUnprocessableEntity, fmt.Sprintf("error getting TFOResourceSpec: %v", result.Error), nil))
return
}
if tfoResourceSpec.ID == 0 {
c.JSON(http.StatusNotFound, response(http.StatusNotFound, "TFOResourceSpec not found", nil))
return
}

apiURL := GetApiURL(c, h.serviceIP)
_, err := NewTaskToken(h.DB, tfoResourceSpec, h.tenant, clusterName, apiURL, h.clientset)
if err != nil {

c.JSON(http.StatusUnprocessableEntity, response(http.StatusUnprocessableEntity, err.Error(), nil))
return
}

c.JSON(http.StatusNoContent, response(http.StatusNoContent, "", nil))
}

// Soft deletes tfo_resources from the database except the latest one via created_at timestamp
func deleteTFOResourcesExceptNewest(db *gorm.DB, tfoResource *models.TFOResource) error {
// A tfo resource has a namespace and a name, which are used to identify it uniquely within the cluster.
Expand Down
Loading