Skip to content

Commit

Permalink
Add ability to configure the shared SSO while adding project (#745)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:

**Which issue(s) this PR fixes**:

Fixes #

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
-->
```release-note
NONE
```

This PR was merged by Kapetanios.
  • Loading branch information
nghialv committed Sep 5, 2020
1 parent c7c50e6 commit d3d75bd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/app/operator/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *server) run(ctx context.Context, t cli.Telemetry) error {

// Start running HTTP server.
{
handler := handler.NewHandler(s.httpPort, datastore.NewProjectStore(ds), s.gracePeriod, t.Logger)
handler := handler.NewHandler(s.httpPort, datastore.NewProjectStore(ds), cfg.SharedSSOConfigs, s.gracePeriod, t.Logger)
group.Go(func() error {
return handler.Run(ctx)
})
Expand Down
1 change: 1 addition & 0 deletions pkg/app/operator/handler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
importpath = "github.com/pipe-cd/pipe/pkg/app/operator/handler",
visibility = ["//visibility:public"],
deps = [
"//pkg/config:go_default_library",
"//pkg/datastore:go_default_library",
"//pkg/model:go_default_library",
"@org_uber_go_zap//:go_default_library",
Expand Down
46 changes: 33 additions & 13 deletions pkg/app/operator/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"go.uber.org/zap"

"github.com/pipe-cd/pipe/pkg/config"
"github.com/pipe-cd/pipe/pkg/datastore"
"github.com/pipe-cd/pipe/pkg/model"
)
Expand All @@ -41,17 +42,19 @@ type projectStore interface {
}

type Handler struct {
port int
projectStore projectStore
server *http.Server
gracePeriod time.Duration
logger *zap.Logger
port int
projectStore projectStore
sharedSSOConfigs []config.SharedSSOConfig
server *http.Server
gracePeriod time.Duration
logger *zap.Logger
}

func NewHandler(port int, ps projectStore, gracePeriod time.Duration, logger *zap.Logger) *Handler {
func NewHandler(port int, ps projectStore, sharedSSOConfigs []config.SharedSSOConfig, gracePeriod time.Duration, logger *zap.Logger) *Handler {
mux := http.NewServeMux()
h := &Handler{
projectStore: ps,
projectStore: ps,
sharedSSOConfigs: sharedSSOConfigs,
server: &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
Expand Down Expand Up @@ -134,6 +137,7 @@ func (h *Handler) handleListProjects(w http.ResponseWriter, r *http.Request) {
"ID": projects[i].Id,
"Description": projects[i].Desc,
"StaticAdminDisabled": strconv.FormatBool(projects[i].StaticAdminDisabled),
"SharedSSOName": projects[i].SharedSsoName,
"CreatedAt": time.Unix(projects[i].CreatedAt, 0).String(),
})
}
Expand All @@ -155,18 +159,33 @@ func (h *Handler) handleAddProject(w http.ResponseWriter, r *http.Request) {
}

var (
id = r.FormValue("ID")
description = r.FormValue("Description")
id = r.FormValue("ID")
description = r.FormValue("Description")
sharedSSOName = r.FormValue("SharedSSO")
)
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
return
}
if sharedSSOName != "" {
found := false
for i := range h.sharedSSOConfigs {
if h.sharedSSOConfigs[i].Name == sharedSSOName {
found = true
break
}
}
if !found {
http.Error(w, fmt.Sprintf("SharedSSOConfig %q was not found in Control Plane configuration", sharedSSOName), http.StatusBadRequest)
return
}
}

var (
project = &model.Project{
Id: id,
Desc: description,
Id: id,
Desc: description,
SharedSsoName: sharedSSOName,
}
username = model.GenerateRandomString(10)
password = model.GenerateRandomString(30)
Expand All @@ -177,7 +196,7 @@ func (h *Handler) handleAddProject(w http.ResponseWriter, r *http.Request) {
zap.String("id", id),
zap.Error(err),
)
http.Error(w, "Unable to add the project", http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Unable to add the project (%v)", err), http.StatusInternalServerError)
return
}

Expand All @@ -189,14 +208,15 @@ func (h *Handler) handleAddProject(w http.ResponseWriter, r *http.Request) {
zap.String("id", id),
zap.Error(err),
)
http.Error(w, "Unable to add the project", http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Unable to add the project (%v)", err), http.StatusInternalServerError)
return
}
h.logger.Info("successfully added a new project", zap.String("id", id))

data := map[string]string{
"ID": id,
"Description": description,
"SharedSSOName": sharedSSOName,
"StaticAdminUsername": username,
"StaticAdminPassword": password,
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/app/operator/handler/templates/AddProject
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<style>
label {
display: block;
width: 6em;
float: left;
}
</style>
</head>
<body>

<h2 style="text-align: center;"><a href="/">Welcome to PipeCD Owner Page!</a></h2>

<h3>Add a new project</h3>

<form method="POST">
<label>ID:</label><br>
<label>ID (Required)</label>
<input type="text" name="ID"><br><br>
<label>Description:</label><br>
<label>Description</label>
<input type="text" name="Description"><br><br>
<label>Shared SSO</label>
<input type="text" name="SharedSSO"><br><br>
<input type="submit">
</form>

Expand Down
4 changes: 4 additions & 0 deletions pkg/app/operator/handler/templates/AddedProject
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ tr:nth-child(1) {
<td>Description</td>
<td>{{ .Description }}</td>
</tr>
<tr>
<td>Shared SSO Name</td>
<td>{{ .SharedSSOName }}</td>
</tr>
<tr>
<td>Static Admin Username</td>
<td>{{ .StaticAdminUsername }}</td>
Expand Down
2 changes: 2 additions & 0 deletions pkg/app/operator/handler/templates/ListProjects
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tr:nth-child(1) {
<th>ID</th>
<th>Description</th>
<th>Static Admin Disabled</th>
<th>Shared SSO Name</th>
<th>Created At</th>
</tr>
{{ range $index, $project := . }}
Expand All @@ -39,6 +40,7 @@ tr:nth-child(1) {
<td>{{ $project.ID }}</td>
<td>{{ $project.Description }}</td>
<td>{{ $project.StaticAdminDisabled }}</td>
<td>{{ $project.SharedSSOName }}</td>
<td>{{ $project.CreatedAt }}</td>
</tr>
{{ end }}
Expand Down

0 comments on commit d3d75bd

Please sign in to comment.