Skip to content

Commit

Permalink
Added GetUserID() and GetAPIToken() to read env vars when necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Nov 28, 2023
1 parent 08c5f9a commit e9288b8
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 41 deletions.
4 changes: 2 additions & 2 deletions core/aptrust_client_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func (client *APTrustClientV3) connect() error {
registryClient, err := apt_network.NewRegistryClient(
client.config.Url,
client.version,
client.config.UserID,
client.config.APIToken,
client.config.GetUserID(),
client.config.GetAPIToken(),
Dart.Log,
)
client.registry = registryClient
Expand Down
23 changes: 0 additions & 23 deletions core/remote_repo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,3 @@ func GetRemoteRepoClient(repo *RemoteRepository) (RemoteRepoClient, error) {
}
return constructor(repo), nil
}

// // GetViableRepoClients returns a list of RemoteRepoClients that have
// // proven they can connect to the repositories they're supposed to talk to.
// func GetViableRepoClients() ([]RemoteRepoClient, error) {
// clients := make([]RemoteRepoClient, 0)
// result := ObjList(constants.TypeRemoteRepository, "obj_name", 100, 0)
// if result.Error != nil {
// Dart.Log.Errorf("GetViableRepoClients - Error fetching repo list: %s", result.Error.Error())
// return nil, result.Error
// }
// for _, repo := range result.RemoteRepositories {
// err := repo.TestConnection()
// if err == nil {
// newClient, clientErr := GetRemoteRepoClient(repo)
// if clientErr != nil {
// Dart.Log.Errorf("GetViableRepoClients - Error creating client for repo '%s': %s", repo.Name, clientErr.Error())
// } else {
// clients = append(clients, newClient)
// }
// }
// }
// return clients, nil
// }
56 changes: 56 additions & 0 deletions core/remote_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"os"
"strings"

"github.com/APTrust/dart-runner/constants"
Expand Down Expand Up @@ -38,6 +39,44 @@ func NewRemoteRepository() *RemoteRepository {
}
}

// GetUserID returns the UserID for logging into this remote repo.
// If the UserID comes from an environment variable, this returns
// the value of the ENV var. Otherwise, it returns the value of
// this object's UserID property.
//
// When authenticating with a remote repo, call this instead of
// accessing UserID directly.
func (repo *RemoteRepository) GetUserID() string {
if strings.HasPrefix(repo.UserID, "env:") {
parts := strings.SplitN(repo.UserID, ":", 2)
userID := os.Getenv(parts[1])
if userID == "" {
Dart.Log.Warningf("UserID for repo '%s' is set to env var '%s', but the env var has no value", repo.Name, parts[1])
}
return userID
}
return repo.UserID
}

// GetUserAPIToken returns the API token for logging into this remote repo.
// If the token comes from an environment variable, this returns
// the value of the ENV var. Otherwise, it returns the value of
// this object's APIToken property.
//
// When authenticating with a remote repo, call this instead of
// accessing APIToken directly.
func (repo *RemoteRepository) GetAPIToken() string {
if strings.HasPrefix(repo.APIToken, "env:") {
parts := strings.SplitN(repo.APIToken, ":", 2)
token := os.Getenv(parts[1])
if token == "" {
Dart.Log.Warningf("API token for repo '%s' is set to env var '%s', but the env var has no value", repo.Name, parts[1])
}
return token
}
return repo.APIToken
}

// HasPlaintextAPIToken returns true if this repo's API token
// is non-empty and does not come from an environment variable.
func (repo *RemoteRepository) HasPlaintextAPIToken() bool {
Expand Down Expand Up @@ -120,3 +159,20 @@ func (repo *RemoteRepository) TestConnection() error {
}
return client.TestConnection()
}

// ReportsAvailable returns a list of reports that can be retrieved
// from this remote repository.
func (repo *RemoteRepository) ReportsAvailable() ([]util.NameValuePair, error) {
reports := make([]util.NameValuePair, 0)
if util.LooksLikeUUID(repo.PluginID) {
return reports, fmt.Errorf("no client exists for this repo because plugin id is not a uuid")
}
client, err := GetRemoteRepoClient(repo)
if err != nil {
return reports, err
}
if client == nil {
return reports, fmt.Errorf("client for repo %s is nil", repo.Name)
}
return client.AvailableHTMLReports(), nil
}
23 changes: 23 additions & 0 deletions core/remote_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core_test

import (
"database/sql"
"os"
"testing"

"github.com/APTrust/dart-runner/constants"
Expand Down Expand Up @@ -149,3 +150,25 @@ func TestHasPlaintextAPIToken(t *testing.T) {
repo.APIToken = "this-here-is-secret"
assert.True(t, repo.HasPlaintextAPIToken())
}

func TestRepoGetUserID(t *testing.T) {
os.Setenv("dart-repo-user-test-id", "Helen Talboys")
defer os.Unsetenv("dart-repo-user-test-id")
repo := &core.RemoteRepository{}
repo.UserID = "Lucy Graham"
assert.Equal(t, "Lucy Graham", repo.GetUserID())

repo.UserID = "env:dart-repo-user-test-id"
assert.Equal(t, "Helen Talboys", repo.GetUserID())
}

func TestRepoGetAPIToken(t *testing.T) {
os.Setenv("dart-repo-test-token", "George Talboys")
defer os.Unsetenv("dart-repo-test-token")
repo := &core.RemoteRepository{}
repo.APIToken = "Robert Audley"
assert.Equal(t, "Robert Audley", repo.GetAPIToken())

repo.APIToken = "env:dart-repo-test-token"
assert.Equal(t, "George Talboys", repo.GetAPIToken())
}
46 changes: 44 additions & 2 deletions server/controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package controllers

import (
"encoding/json"
"net/http"

"github.com/APTrust/dart-runner/constants"
"github.com/APTrust/dart-runner/core"
"github.com/APTrust/dart-runner/util"
"github.com/gin-gonic/gin"
)

type DashboardReport struct {
RepositoryID string `json:"repositoryId"`
RepositoryName string `json:"repositoryName"`
ReportName string `json:"reportName"`
ReportDescription string `json:"reportDescription"`
}

// DashboardShow shows the DART dashboard. This is essentially
// DART's homepage.
//
Expand All @@ -22,10 +31,16 @@ func DashboardShow(c *gin.Context) {
return
}

// TODO: Repo ids and report names
var reportListJson []byte
reports, err := getAvailableReports()
if len(reports) > 0 {
reportListJson, err = json.Marshal(reports)
}

data := gin.H{
"jobs": result.Jobs,
"jobs": result.Jobs,
"reports": string(reportListJson),
"reportsErr": err,
}

c.HTML(http.StatusOK, "dashboard/show.html", data)
Expand Down Expand Up @@ -59,6 +74,33 @@ func DashboardGetReport(c *gin.Context) {
c.JSON(status, data)
}

func getAvailableReports() ([]DashboardReport, error) {
reports := make([]DashboardReport, 0)
result := core.ObjList(constants.TypeRemoteRepository, "obj_name", 100, 0)
if result.Error != nil {
return reports, result.Error
}
for _, repo := range result.RemoteRepositories {
if util.LooksLikeUUID(repo.PluginID) {
available, err := repo.ReportsAvailable()
if err != nil {
core.Dart.Log.Errorf("Error getting report list for repo %s (%s): %v", repo.Name, repo.ID, err)
continue
}
for _, report := range available {
report := DashboardReport{
RepositoryID: repo.ID,
RepositoryName: repo.Name,
ReportName: report.Name,
ReportDescription: report.Value,
}
reports = append(reports, report)
}
}
}
return reports, nil
}

func getRepoReport(remoteRepoID, reportName string) (string, error) {
result := core.ObjFind(remoteRepoID)
if result.Error != nil {
Expand Down
22 changes: 8 additions & 14 deletions server/views/dashboard/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,18 @@
</div>
</div>

{{ range $index, $reports := .reportRows }}

<div class="row mb-3">
<!-- Reports from remote repos will go in here -->
<div id="reportsContainer"></div>

{{ range $ix, $report := .reports }}
<div class="col-md-6">
<div class="card">
<div class="card-header">{{ $report.title }}</div>
<div class="card-body dashboard-card-body" id="{{ $report.id }}">

</div>
</div>
</div>
{{ end }}

</div>
<script>
$(function(){
var repoReports = `{{ .reportListJson }}`

})
</script>

{{ end }}

{{ template "partials/page_footer.html" .}}

Expand Down

0 comments on commit e9288b8

Please sign in to comment.