2011-01-28 07:07:18 +00:00
/ *
Copyright 2011 Google Inc .
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 .
* /
2011-01-02 22:36:03 +00:00
package client
2010-12-20 22:54:41 +00:00
import (
"flag"
2011-05-30 19:38:26 +00:00
"io/ioutil"
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"log"
2011-01-17 05:16:26 +00:00
"os"
2011-03-16 06:16:24 +00:00
"path/filepath"
2010-12-20 22:54:41 +00:00
"strings"
2011-01-17 05:16:26 +00:00
"sync"
2011-04-02 05:26:33 +00:00
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"camlistore.org/pkg/auth"
"camlistore.org/pkg/blobref"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/pkg/jsonsign"
"camlistore.org/pkg/osutil"
2010-12-20 22:54:41 +00:00
)
2012-04-13 02:36:00 +00:00
// These, if set, override the JSON config file ~/.camlistore/config
2011-10-11 00:38:18 +00:00
// "server" and "password" keys.
//
// A main binary must call AddFlags to expose these.
2011-11-16 10:41:38 +00:00
var flagServer * string
2011-10-11 00:38:18 +00:00
func AddFlags ( ) {
2013-01-26 00:23:31 +00:00
defaultPath := ConfigFilePath ( )
flagServer = flag . String ( "server" , "" , "Camlistore server prefix. If blank, the default from the \"server\" field of " + defaultPath + " is used. Acceptable forms: https://you.example.com, example.com:1345 (https assumed), or http://you.example.com/alt-root" )
2011-10-11 00:38:18 +00:00
}
2010-12-20 22:54:41 +00:00
2013-01-02 20:55:12 +00:00
// ExplicitServer returns the blobserver given in the flags, if any.
func ExplicitServer ( ) string {
if flagServer != nil {
return * flagServer
}
return ""
}
2011-01-18 02:28:38 +00:00
func ConfigFilePath ( ) string {
2011-04-02 05:26:33 +00:00
return filepath . Join ( osutil . CamliConfigDir ( ) , "config" )
2011-01-17 05:16:26 +00:00
}
var configOnce sync . Once
var config = make ( map [ string ] interface { } )
2013-01-27 00:46:19 +00:00
var parseConfigErr error
2011-05-10 19:56:40 +00:00
2011-01-17 05:16:26 +00:00
func parseConfig ( ) {
2011-05-30 19:38:26 +00:00
configPath := ConfigFilePath ( )
2013-01-27 00:46:19 +00:00
if _ , err := os . Stat ( configPath ) ; os . IsNotExist ( err ) {
parseConfigErr = os . ErrNotExist
return
}
2011-06-24 20:02:51 +00:00
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
var err error
2011-06-24 20:02:51 +00:00
if config , err = jsonconfig . ReadFile ( configPath ) ; err != nil {
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
log . Fatal ( err . Error ( ) )
2011-01-17 05:16:26 +00:00
return
2011-05-30 19:38:26 +00:00
}
2011-01-17 05:16:26 +00:00
}
2010-12-20 22:54:41 +00:00
func cleanServer ( server string ) string {
// Remove trailing slash if provided.
if strings . HasSuffix ( server , "/" ) {
server = server [ 0 : len ( server ) - 1 ]
}
2011-11-16 10:41:38 +00:00
// Default to "https://" when not specified
if ! strings . HasPrefix ( server , "http" ) && ! strings . HasPrefix ( server , "https" ) {
server = "https://" + server
2010-12-20 22:54:41 +00:00
}
return server
}
2013-01-26 00:23:31 +00:00
func serverOrDie ( ) string {
2011-10-11 00:38:18 +00:00
if flagServer != nil && * flagServer != "" {
2010-12-20 22:54:41 +00:00
return cleanServer ( * flagServer )
}
2011-01-17 05:16:26 +00:00
configOnce . Do ( parseConfig )
2013-01-26 00:23:31 +00:00
value , ok := config [ "server" ]
2011-01-18 02:28:38 +00:00
var server string
if ok {
server = value . ( string )
}
server = cleanServer ( server )
if ! ok || server == "" {
2013-01-26 00:23:31 +00:00
log . Fatalf ( "Missing or invalid \"server\" in %q" , ConfigFilePath ( ) )
2011-02-02 20:27:30 +00:00
}
2011-01-18 02:28:38 +00:00
return server
2010-12-20 22:54:41 +00:00
}
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
func ( c * Client ) SetupAuth ( ) error {
2013-01-02 19:42:39 +00:00
if flagServer != nil && * flagServer != "" {
// If using an explicit blobserver, don't use auth
// configured from the config file, so we don't send
// our password to a friend's blobserver.
2013-01-26 00:23:31 +00:00
var err error
c . authMode , err = auth . FromEnv ( )
2013-01-26 19:23:39 +00:00
if err == auth . ErrNoAuth {
log . Printf ( "Using explicit --server parameter; not using config file auth, and no auth mode set in environment" )
}
2013-01-26 00:23:31 +00:00
return err
2013-01-02 19:42:39 +00:00
}
2013-01-27 00:46:19 +00:00
configOnce . Do ( parseConfig )
2011-11-16 10:41:38 +00:00
return c . SetupAuthFromConfig ( config )
}
2012-11-08 03:11:12 +00:00
func ( c * Client ) SetupAuthFromConfig ( conf jsonconfig . Obj ) error {
var err error
2011-11-16 10:41:38 +00:00
value , ok := conf [ "auth" ]
authString := ""
2011-01-18 02:28:38 +00:00
if ok {
2011-11-16 10:41:38 +00:00
authString , ok = value . ( string )
2011-12-02 10:35:28 +00:00
c . authMode , err = auth . FromConfig ( authString )
} else {
c . authMode , err = auth . FromEnv ( )
2011-01-18 02:28:38 +00:00
}
2011-12-02 10:35:28 +00:00
return err
2010-12-20 22:54:41 +00:00
}
2011-01-17 05:16:26 +00:00
// Returns blobref of signer's public key, or nil if unconfigured.
func ( c * Client ) SignerPublicKeyBlobref ( ) * blobref . BlobRef {
2011-03-16 06:16:24 +00:00
return SignerPublicKeyBlobref ( )
}
2011-05-30 19:38:26 +00:00
func ( c * Client ) SecretRingFile ( ) string {
configOnce . Do ( parseConfig )
keyRing , ok := config [ "secretRing" ] . ( string )
if ok && keyRing != "" {
return keyRing
}
2012-04-13 02:36:00 +00:00
if keyRing = osutil . IdentitySecretRing ( ) ; fileExists ( keyRing ) {
return keyRing
}
2011-05-30 19:38:26 +00:00
return jsonsign . DefaultSecRingPath ( )
}
2012-04-13 02:36:00 +00:00
func fileExists ( name string ) bool {
_ , err := os . Stat ( name )
return err == nil
}
2012-08-04 23:25:21 +00:00
var (
signerPublicKeyRefOnce sync . Once
2012-10-09 14:47:43 +00:00
signerPublicKeyRef * blobref . BlobRef
2012-08-04 23:25:21 +00:00
)
2011-03-16 06:16:24 +00:00
// TODO: move to config package?
func SignerPublicKeyBlobref ( ) * blobref . BlobRef {
2012-08-04 23:25:21 +00:00
signerPublicKeyRefOnce . Do ( initSignerPublicKeyBlobref )
return signerPublicKeyRef
}
func initSignerPublicKeyBlobref ( ) {
signerPublicKeyRef = getSignerPublicKeyBlobref ( )
}
func getSignerPublicKeyBlobref ( ) * blobref . BlobRef {
2011-01-17 05:16:26 +00:00
configOnce . Do ( parseConfig )
2011-05-30 19:38:26 +00:00
key := "keyId"
keyId , ok := config [ key ] . ( string )
2011-01-17 05:16:26 +00:00
if ! ok {
2011-09-26 00:40:01 +00:00
log . Printf ( "No key %q in JSON configuration file %q; have you run \"camput init\"?" , key , ConfigFilePath ( ) )
2011-01-17 05:16:26 +00:00
return nil
}
2012-04-13 02:36:00 +00:00
keyRing , hasKeyRing := config [ "secretRing" ] . ( string )
if ! hasKeyRing {
if fn := osutil . IdentitySecretRing ( ) ; fileExists ( fn ) {
keyRing = fn
} else if fn := jsonsign . DefaultSecRingPath ( ) ; fileExists ( fn ) {
keyRing = fn
} else {
log . Printf ( "Couldn't find keyId %q; no 'secretRing' specified in config file, and no standard secret ring files exist." )
return nil
}
}
2011-05-30 19:38:26 +00:00
entity , err := jsonsign . EntityFromSecring ( keyId , keyRing )
if err != nil {
2011-06-02 23:57:49 +00:00
log . Printf ( "Couldn't find keyId %q in secret ring: %v" , keyId , err )
2011-05-30 19:38:26 +00:00
return nil
}
armored , err := jsonsign . ArmoredPublicKey ( entity )
if err != nil {
log . Printf ( "Error serializing public key: %v" , err )
return nil
}
selfPubKeyDir , ok := config [ "selfPubKeyDir" ] . ( string )
2011-01-17 05:16:26 +00:00
if ! ok {
2011-05-30 19:38:26 +00:00
log . Printf ( "No 'selfPubKeyDir' defined in %q" , ConfigFilePath ( ) )
return nil
}
fi , err := os . Stat ( selfPubKeyDir )
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
if err != nil || ! fi . IsDir ( ) {
2011-05-30 19:38:26 +00:00
log . Printf ( "selfPubKeyDir of %q doesn't exist or not a directory" , selfPubKeyDir )
return nil
2011-01-17 05:16:26 +00:00
}
2011-05-30 19:38:26 +00:00
2011-11-27 19:06:12 +00:00
br := blobref . SHA1FromString ( armored )
2011-05-30 19:38:26 +00:00
2011-07-02 16:09:50 +00:00
pubFile := filepath . Join ( selfPubKeyDir , br . String ( ) + ".camli" )
2011-05-30 19:38:26 +00:00
fi , err = os . Stat ( pubFile )
if err != nil {
err = ioutil . WriteFile ( pubFile , [ ] byte ( armored ) , 0644 )
if err != nil {
log . Printf ( "Error writing public key to %q: %v" , pubFile , err )
return nil
}
2011-01-17 05:16:26 +00:00
}
2011-05-30 19:38:26 +00:00
return br
2011-01-17 05:16:26 +00:00
}
2011-06-04 15:56:03 +00:00
func ( c * Client ) GetBlobFetcher ( ) blobref . SeekFetcher {
2011-03-16 06:16:24 +00:00
// Use blobref.NewSeriesFetcher(...all configured fetch paths...)
return blobref . NewConfigDirFetcher ( )
2011-01-17 05:16:26 +00:00
}