func (c *initCmd) clientConfigFromServer() (*clientconfig.Config, error) { if c.noconfig { log.Print("--userpass and --noconfig are mutually exclusive") return nil, cmdmain.ErrUsage } server := client.ExplicitServer() if server == "" { log.Print("--userpass requires --server") return nil, cmdmain.ErrUsage } fields := strings.Split(c.userPass, ":") if len(fields) != 2 { log.Printf("wrong userpass; wanted username:password, got %q", c.userPass) return nil, cmdmain.ErrUsage } cl := client.NewFromParams(server, auth.NewBasicAuth(fields[0], fields[1]), client.OptionInsecure(c.insecureTLS)) helpRoot, err := cl.HelpRoot() if err != nil { return nil, err } var cc clientconfig.Config if err := cl.GetJSON(helpRoot+"?clientConfig=true", &cc); err != nil { return nil, err } return &cc, nil }
func NewHandler(cfg HandlerConfig) (*Handler, error) { name := cfg.Program if cfg.Prefix == "" { return nil, fmt.Errorf("app: could not initialize Handler for %q: empty Prefix", name) } listen, backendURL, apiHost := cfg.Listen, cfg.BackendURL, cfg.APIHost var err error if listen == "" { if cfg.ServerListen == "" { return nil, fmt.Errorf(`app: could not initialize Handler for %q: neither "Listen" or "ServerListen" defined`, name) } listen, err = randListen(cfg.ServerListen) if err != nil { return nil, err } } if backendURL == "" { if cfg.ServerBaseURL == "" { return nil, fmt.Errorf(`app: could not initialize Handler for %q: neither "BackendURL" or "ServerBaseURL" defined`, name) } backendURL, err = baseURL(cfg.ServerBaseURL, listen) if err != nil { return nil, err } } if apiHost == "" { if cfg.ServerBaseURL == "" { return nil, fmt.Errorf(`app: could not initialize Handler for %q: neither "APIHost" or "ServerBaseURL" defined`, name) } apiHost = cfg.ServerBaseURL + "/" } proxyURL, err := url.Parse(backendURL) if err != nil { return nil, fmt.Errorf("could not parse backendURL %q: %v", backendURL, err) } username, password := auth.RandToken(20), auth.RandToken(20) camliAuth := username + ":" + password basicAuth := auth.NewBasicAuth(username, password) envVars := map[string]string{ "CAMLI_API_HOST": apiHost, "CAMLI_AUTH": camliAuth, "CAMLI_APP_LISTEN": listen, } if cfg.AppConfig != nil { envVars["CAMLI_APP_CONFIG_URL"] = apiHost + strings.TrimPrefix(cfg.Prefix, "/") + "config.json" } return &Handler{ name: name, envVars: envVars, auth: basicAuth, appConfig: cfg.AppConfig, prefix: strings.TrimSuffix(cfg.Prefix, "/"), proxy: httputil.NewSingleHostReverseProxy(proxyURL), backendURL: backendURL, }, nil }
// NewHandler returns a Handler that proxies requests to an app. Start() on the // Handler starts the app. // The apiHost must end in a slash and is the camlistored API server for the app // process to hit. // The appHandlerPrefix is the URL path prefix on apiHost where the app is mounted. // It must end in a slash, and be at minimum "/". // The conf object has the following members, related to the vars described in // doc/app-environment.txt: // "program", string, required. File name of the app's program executable. Either // an absolute path, or the name of a file located in CAMLI_APP_BINDIR or in PATH. // "backendURL", string, optional. Automatic if absent. It sets CAMLI_APP_BACKEND_URL. // "appConfig", object, optional. Additional configuration that the app can request from Camlistore. func NewHandler(conf jsonconfig.Obj, apiHost, appHandlerPrefix string) (*Handler, error) { // TODO: remove the appHandlerPrefix if/when we change where the app config JSON URL is made available. name := conf.RequiredString("program") backendURL := conf.OptionalString("backendURL", "") appConfig := conf.OptionalObject("appConfig") // TODO(mpl): add an auth token in the extra config of the dev server config, // that the hello app can use to setup a status handler than only responds // to requests with that token. if err := conf.Validate(); err != nil { return nil, err } if apiHost == "" { return nil, fmt.Errorf("app: could not initialize Handler for %q: Camlistore apiHost is unknown", name) } if appHandlerPrefix == "" { return nil, fmt.Errorf("app: could not initialize Handler for %q: empty appHandlerPrefix", name) } if backendURL == "" { var err error // If not specified in the conf, we're dynamically picking the port of the CAMLI_APP_BACKEND_URL // now (instead of letting the app itself do it), because we need to know it in advance in order // to set the app handler's proxy. backendURL, err = randPortBackendURL(apiHost, appHandlerPrefix) if err != nil { return nil, err } } username, password := auth.RandToken(20), auth.RandToken(20) camliAuth := username + ":" + password basicAuth := auth.NewBasicAuth(username, password) envVars := map[string]string{ "CAMLI_API_HOST": apiHost, "CAMLI_AUTH": camliAuth, "CAMLI_APP_BACKEND_URL": backendURL, } if appConfig != nil { envVars["CAMLI_APP_CONFIG_URL"] = apiHost + strings.TrimPrefix(appHandlerPrefix, "/") + "config.json" } proxyURL, err := url.Parse(backendURL) if err != nil { return nil, fmt.Errorf("could not parse backendURL %q: %v", backendURL, err) } return &Handler{ name: name, envVars: envVars, auth: basicAuth, appConfig: appConfig, proxy: httputil.NewSingleHostReverseProxy(proxyURL), backendURL: backendURL, }, nil }
func basicAuth() (auth.AuthMode, error) { 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) } return auth.NewBasicAuth(userpass[0], userpass[1]), nil }
// Client returns a client from pkg/client, configured by environment variables // for applications, and ready to be used to connect to the Camlistore server. func Client() (*client.Client, error) { server := os.Getenv("CAMLI_API_HOST") if server == "" { return nil, errors.New("CAMLI_API_HOST var not set") } 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) } cl := client.NewFromParams(server, auth.NewBasicAuth(userpass[0], userpass[1])) return cl, nil }
func main() { fmt.Printf("oi\n") camliAuth := auth.NewBasicAuth("nictuku", "SOMETHING") client := camli.NewFromParams("http://<IP>:3179", camliAuth) client.InsecureTLS = true recent, err := client.GetRecentPermanodes(&search.RecentRequest{}) if err != nil { log.Printf("error getting recent permanodes:", err) os.Exit(1) } for _, desc := range recent.Meta { fmt.Printf("title: %v\ndescription: %v\n--\n", desc.Title(), desc.Description()) } fmt.Println("printed %d items", len(recent.Meta)) }