2014-05-08 14:07:29 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2014 The Perkeep Authors.
|
2014-05-08 14:07:29 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package app provides helpers for server applications interacting
|
2018-01-30 11:02:56 +00:00
|
|
|
// with Perkeep.
|
2016-06-28 00:12:47 +00:00
|
|
|
// See also https://camlistore.org/doc/app-environment for the related
|
|
|
|
// variables.
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package app // import "perkeep.org/pkg/app"
|
2014-05-08 14:07:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-06-28 00:12:47 +00:00
|
|
|
"net/http"
|
2014-05-08 14:07:29 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/auth"
|
|
|
|
"perkeep.org/pkg/client"
|
2014-05-08 14:07:29 +00:00
|
|
|
)
|
|
|
|
|
2018-01-30 11:02:56 +00:00
|
|
|
// Auth returns the auth mode for the app to access Perkeep, as defined by
|
|
|
|
// environment variables automatically supplied by the Perkeep server host.
|
pkg/server/app: proxy search requests for publisher
Some of the publisher features have moved from the server-side app to
the client-side app (the browser) thanks to gopherjs. Some of these
features imply doing some search queries against Camlistore, which
requires authentication. The server-side app receives the necessary
credentials on creation, from Camlistore. However, we can't just
communicate them to the client-side (as we do with the web UI) since the
publisher app itself does not require any auth and is supposed to be
exposed to the world.
Therefore, we need to allow some search queries to be done without
authentication.
To this end, the app handler on Camlistore now assumes a new role: it is
also a search proxy for the app. The app sends an unauthenticated search
query to the app handler (instead of directly to the search handler),
and it is the role of the app handler to verify that this query is
allowed for the app, and if yes, to forward the search to the Camlistore's
search handler.
We introduce a new mechanism to filter the search queries in the form of
a master query. Upon startup, the publisher registers, using the new
CAMLI_APP_MASTERQUERY_URL env var, a *search.SearchQuery with the app
handler. The app handler runs that query and caches all the blob refs
included in the response to that query. In the following, all incoming
search queries are run by the app handler, which checks that none of the
response blobs are out of the set defined by the aforementioned cached
blob refs. If that check fails, the search response is not forwarded to
the app/client.
The process can be improved in a subsequent CL (or patchset), with finer
grained domains, i.e. a master search query per published camliPath,
instead of one for the whole app handler.
Change-Id: I00d91ff73e0cbe78744bfae9878077dc3a8521f4
2016-07-20 15:54:29 +00:00
|
|
|
func Auth() (auth.AuthMode, error) {
|
|
|
|
return basicAuth()
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:12:47 +00:00
|
|
|
func basicAuth() (auth.AuthMode, error) {
|
2014-05-08 14:07:29 +00:00
|
|
|
authString := os.Getenv("CAMLI_AUTH")
|
|
|
|
if authString == "" {
|
|
|
|
return nil, errors.New("CAMLI_AUTH var not set")
|
|
|
|
}
|
|
|
|
userpass := strings.Split(authString, ":")
|
|
|
|
if len(userpass) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid auth string syntax. got %q, want \"username:password\"", authString)
|
|
|
|
}
|
2016-06-28 00:12:47 +00:00
|
|
|
return auth.NewBasicAuth(userpass[0], userpass[1]), nil
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:02:56 +00:00
|
|
|
// Client returns a Perkeep client as defined by environment variables
|
|
|
|
// automatically supplied by the Perkeep server host.
|
2016-06-28 00:12:47 +00:00
|
|
|
func Client() (*client.Client, error) {
|
|
|
|
server := os.Getenv("CAMLI_API_HOST")
|
|
|
|
if server == "" {
|
|
|
|
return nil, errors.New("CAMLI_API_HOST var not set")
|
|
|
|
}
|
|
|
|
am, err := basicAuth()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-24 00:38:26 +00:00
|
|
|
return client.New(
|
|
|
|
client.OptionNoExternalConfig(),
|
|
|
|
client.OptionServer(server),
|
|
|
|
client.OptionAuthMode(am),
|
|
|
|
)
|
2014-05-08 14:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAddress returns the host:[port] network address, derived from the environment,
|
|
|
|
// that the application should listen on.
|
|
|
|
func ListenAddress() (string, error) {
|
2016-06-28 00:12:47 +00:00
|
|
|
listenAddr := os.Getenv("CAMLI_APP_LISTEN")
|
|
|
|
if listenAddr == "" {
|
|
|
|
return "", errors.New("CAMLI_APP_LISTEN is undefined")
|
|
|
|
}
|
|
|
|
return listenAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathPrefix returns the app's prefix on the app handler if the request was proxied
|
2018-01-30 11:02:56 +00:00
|
|
|
// through Perkeep, or "/" if the request went directly to the app.
|
2016-06-28 00:12:47 +00:00
|
|
|
func PathPrefix(r *http.Request) string {
|
|
|
|
if prefix := httputil.PathBase(r); prefix != "" {
|
|
|
|
return prefix
|
2014-05-08 14:07:29 +00:00
|
|
|
}
|
2016-06-28 00:12:47 +00:00
|
|
|
return "/"
|
2014-05-08 14:07:29 +00:00
|
|
|
}
|