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

Permit to use server under reverse proxy with a custom path #209

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type registryController struct {
l sync.Mutex
tmpl *template.Template
generateOnly bool
PathPrefix string
}

type v1Compatibility struct {
Expand All @@ -51,6 +52,7 @@ type AnalysisResult struct {
Name string `json:"name"`
LastUpdated string `json:"lastUpdated"`
HasVulns bool `json:"hasVulns"`
PathPrefix string `json:"pathPrefix"`
UpdateInterval time.Duration
}

Expand All @@ -64,6 +66,7 @@ func (rc *registryController) repositories(ctx context.Context, staticDir string
RegistryDomain: rc.reg.Domain,
LastUpdated: time.Now().Local().Format(time.RFC1123),
UpdateInterval: rc.interval,
PathPrefix: rc.PathPrefix,
}

repoList, err := rc.reg.Catalog(ctx, "")
Expand Down Expand Up @@ -192,6 +195,7 @@ func (rc *registryController) generateTagsTemplate(ctx context.Context, repo str
LastUpdated: time.Now().Local().Format(time.RFC1123),
UpdateInterval: rc.interval,
Name: repo,
PathPrefix: rc.PathPrefix,
HasVulns: hasVulns, // if we have a clair client we can return vulns
}

Expand Down
48 changes: 24 additions & 24 deletions internal/binutils/static/static.go

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions internal/binutils/templates/templates.go

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (cmd *serverCommand) Register(fs *flag.FlagSet) {
fs.StringVar(&cmd.listenAddress, "listen-address", "", "address to listen on")
fs.StringVar(&cmd.port, "port", "8080", "port for server to run on")
fs.StringVar(&cmd.assetPath, "asset-path", "", "Path to assets and templates")
fs.StringVar(&cmd.prefixPath, "prefix-path", "", "Prefix path of gui. Default /.")

fs.BoolVar(&cmd.generateAndExit, "once", false, "generate the templates once and then exit")
}
Expand All @@ -57,6 +58,7 @@ type serverCommand struct {
listenAddress string
port string
assetPath string
prefixPath string
}

func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
Expand All @@ -70,6 +72,7 @@ func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
rc := registryController{
reg: r,
generateOnly: cmd.generateAndExit,
PathPrefix: cmd.prefixPath,
}

// Create a clair client if the user passed in a server address.
Expand Down Expand Up @@ -150,27 +153,29 @@ func (cmd *serverCommand) Run(ctx context.Context, args []string) error {

// Create mux server.
mux := mux.NewRouter()
mux.StrictSlash(true)
mux.UseEncodedPath()
s := mux.PathPrefix(cmd.prefixPath).Subrouter()

// Static files handler.
mux.HandleFunc("/repo/{repo}/tags", rc.tagsHandler)
mux.HandleFunc("/repo/{repo}/tags/", rc.tagsHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/", rc.vulnerabilitiesHandler)
s.HandleFunc("/repo/{repo}/tags", rc.tagsHandler)
s.HandleFunc("/repo/{repo}/tags/", rc.tagsHandler)
s.HandleFunc("/repo/{repo}/tag/{tag}", rc.vulnerabilitiesHandler)
s.HandleFunc("/repo/{repo}/tag/{tag}/", rc.vulnerabilitiesHandler)

// Add the vulns endpoints if we have a client for a clair server.
if rc.cl != nil {
logrus.Infof("adding clair handlers...")
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns/", rc.vulnerabilitiesHandler)
mux.HandleFunc("/repo/{repo}/tag/{tag}/vulns.json", rc.vulnerabilitiesHandler)
s.HandleFunc("/repo/{repo}/tag/{tag}/vulns", rc.vulnerabilitiesHandler)
s.HandleFunc("/repo/{repo}/tag/{tag}/vulns/", rc.vulnerabilitiesHandler)
s.HandleFunc("/repo/{repo}/tag/{tag}/vulns.json", rc.vulnerabilitiesHandler)
}

// Serve the static assets.
staticAssetsHandler := http.FileServer(static.Assets)
mux.PathPrefix("/static/").Handler(http.StripPrefix("/static/", staticAssetsHandler))
staticHandler := http.FileServer(http.Dir(staticDir))
mux.Handle("/", staticHandler)
s.PathPrefix("/static/").Handler(http.StripPrefix(cmd.prefixPath+"/static/", staticAssetsHandler))
s.PathPrefix("/").Handler(http.StripPrefix(cmd.prefixPath, staticHandler))

// Set up the server.
server := &http.Server{
Expand Down
12 changes: 6 additions & 6 deletions server/templates/repositories.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<base href="/" >
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>{{ .RegistryDomain }}</title>
<link rel="icon" type="image/ico" href="/static/favicon.ico">
<link rel="stylesheet" href="/static/css/styles.css" />
<link rel="icon" type="image/ico" href="{{ .PathPrefix }}/static/favicon.ico">
<link rel="stylesheet" href="{{ .PathPrefix }}/static/css/styles.css" />
</head>
<body>
<h1>{{ .RegistryDomain }}</h1>
Expand All @@ -24,16 +23,17 @@ <h1>{{ .RegistryDomain }}</h1>
<th>Repository Name</th>
<th>Pull Command</th>
</tr>
{{ $pathprefix := .PathPrefix }}
{{ range $key, $value := .Repositories }}
<tr>
<td valign="top">
<a href="/repo/{{ $value.Name | urlquery }}/tags">
<a href="{{ $pathprefix }}/repo/{{ $value.Name | urlquery }}/tags">
{{ $value.Name }}
</a>
</td>

<td align="right" nowrap>
<a href="/repo/{{ $value.Name | urlquery }}/tags">
<a href="{{ $pathprefix }}/repo/{{ $value.Name | urlquery }}/tags">
<code>docker pull {{ $value.URI }}</code>
</a>
</td>
Expand All @@ -48,7 +48,7 @@ <h1>{{ .RegistryDomain }}</h1>
<p>Last Updated: {{ .LastUpdated }}</p>
<p>Update Interval: {{ .UpdateInterval }}</p>
</div><!--/.footer-->
<script src="/static/js/scripts.js"></script>
<script src="{{ .PathPrefix }}/static/js/scripts.js"></script>
</body>
</html>
{{end}}
16 changes: 8 additions & 8 deletions server/templates/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<base href="/" >
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>{{ .RegistryDomain }}/{{ .Name }}</title>
<link rel="icon" type="image/ico" href="/static/favicon.ico">
<link rel="stylesheet" href="/static/css/styles.css" />
<link rel="icon" type="image/ico" href="{{ .PathPrefix }}/static/favicon.ico">
<link rel="stylesheet" href="{{ .PathPrefix }}/static/css/styles.css" />
</head>
<body>
<h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
Expand All @@ -22,15 +21,16 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
<th>Created</th>
{{if .HasVulns}}<th>Vulnerabilities</th>{{end}}
</tr>
{{ $pathprefix := .PathPrefix }}
{{ range $key, $value := .Repositories }}
<tr>
<td valign="left" nowrap>
{{if $.HasVulns}}<a href="/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns">{{end}}
{{if $.HasVulns}}<a href="{{ $pathprefix }}/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns">{{end}}
{{ $value.Name }}
{{if $.HasVulns}}</a>{{end}}
</td>
<td align="right" nowrap>
{{if $.HasVulns}}<a href="/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns">{{end}}
{{if $.HasVulns}}<a href="{{ $pathprefix }}/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns">{{end}}
{{ $value.Tag }}
{{if $.HasVulns}}</a>{{end}}
</td>
Expand All @@ -39,7 +39,7 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
</td>
{{if $.HasVulns}}
<td align="right" nowrap>
<a href="/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns" id="{{ $value.Name }}:{{ $value.Tag }}">
<a href="{{ $pathprefix }}/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns" id="{{ $value.Name }}:{{ $value.Tag }}">
<div class="signal"></div>
</a>
</td>
Expand All @@ -54,12 +54,12 @@ <h1>{{ .RegistryDomain }}/{{ .Name }}</h1>
<p>Checkout the source code at: <a href="https://github.com/genuinetools/reg">github.com/genuinetools/reg</a></p>
<p>Last Updated: {{ .LastUpdated }}</p>
</div><!--/.footer-->
<script src="/static/js/scripts.js"></script>
<script src="{{ .PathPrefix }}/static/js/scripts.js"></script>
{{if .HasVulns}}
<script type="text/javascript">
var ajaxCalls = [
{{ range $key, $value := .Repositories }}
'/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns.json',
'{{ .PathPrefix }}/repo/{{ $value.Name | urlquery }}/tag/{{ $value.Tag }}/vulns.json',
{{ end }}
];
window.onload = function() {
Expand Down
5 changes: 2 additions & 3 deletions server/templates/vulns.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<base href="/" >
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>{{ .RegistryURL }}/{{ .Repo }}:{{ .Tag }} Vulnerability Report</title>
<link rel="icon" type="image/ico" href="/static/favicon.ico">
<link rel="stylesheet" href="/static/css/bootstrap.min.css" />
<link rel="icon" type="image/ico" href="{{ .PathPrefix }}/static/favicon.ico">
<link rel="stylesheet" href="{{ .PathPrefix }}/static/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
Expand Down