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-18 02:28:38 +00:00
package main
import (
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
"encoding/json"
2012-04-13 02:36:00 +00:00
"errors"
2011-01-18 02:28:38 +00:00
"flag"
2011-09-26 00:40:01 +00:00
"fmt"
2011-01-18 02:28:38 +00:00
"io/ioutil"
"log"
2011-12-07 22:41:19 +00:00
"os"
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
"os/exec"
2011-12-07 22:41:19 +00:00
"path"
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/blobref"
"camlistore.org/pkg/client"
"camlistore.org/pkg/jsonsign"
"camlistore.org/pkg/osutil"
2011-01-18 02:28:38 +00:00
)
2011-09-26 00:40:01 +00:00
type initCmd struct {
gpgkey string
}
func init ( ) {
2011-09-27 03:02:35 +00:00
RegisterCommand ( "init" , func ( flags * flag . FlagSet ) CommandRunner {
cmd := new ( initCmd )
flags . StringVar ( & cmd . gpgkey , "gpgkey" , "" , "GPG key to use for signing (overrides $GPGKEY environment)" )
return cmd
} )
2011-09-26 00:40:01 +00:00
}
func ( c * initCmd ) Usage ( ) {
2012-10-21 20:58:06 +00:00
fmt . Fprintf ( stderr , ` Usage : camput init [ opts ]
2011-09-26 00:40:01 +00:00
Initialize the camput configuration file .
` )
}
2011-09-27 03:02:35 +00:00
func ( c * initCmd ) Examples ( ) [ ] string {
return [ ] string {
"" ,
"--gpgkey=XXXXX" ,
2011-09-26 00:40:01 +00:00
}
2011-09-27 03:02:35 +00:00
}
2012-04-13 02:36:00 +00:00
func ( c * initCmd ) keyId ( ) ( string , error ) {
if k := c . gpgkey ; k != "" {
return k , nil
}
if k := os . Getenv ( "GPGKEY" ) ; k != "" {
return k , nil
}
// TODO: move camlistored.go's keyIdFromRing into
// pkg/jsonsign/keys.go and use that (which looks for an
// identify file with exactly one identity)
// TODO: run and parse gpg --list-secret-keys and see if there's just one and suggest that? Or show
// a list of them?
return "" , errors . New ( "Initialization requires your public GPG key. Set --gpgkey=<pubid> or set $GPGKEY in your environment. Run gpg --list-secret-keys to find their key IDs." )
}
func ( c * initCmd ) getPublicKeyArmoredFromFile ( secretRingFileName , keyId string ) ( b [ ] byte , err error ) {
entity , err := jsonsign . EntityFromSecring ( keyId , secretRingFileName )
if err == nil {
pubArmor , err := jsonsign . ArmoredPublicKey ( entity )
if err == nil {
return [ ] byte ( pubArmor ) , nil
}
}
b , err = exec . Command ( "gpg" , "--export" , "--armor" , keyId ) . Output ( )
if err != nil {
return nil , fmt . Errorf ( "Error running gpg to export public key %q: %v" , keyId , err )
}
if len ( b ) == 0 {
return nil , fmt . Errorf ( "gpg export of public key %q was empty." , keyId )
}
return b , nil
}
func ( c * initCmd ) getPublicKeyArmored ( keyId string ) ( b [ ] byte , err error ) {
files := [ ] string { osutil . IdentitySecretRing ( ) , jsonsign . DefaultSecRingPath ( ) }
for _ , file := range files {
b , err = c . getPublicKeyArmoredFromFile ( file , keyId )
if err == nil {
return b , nil
}
}
return nil , fmt . Errorf ( "failed to export armored public key ID %q from locations: %q" , keyId , files )
}
func ( c * initCmd ) RunCommand ( _ * Uploader , args [ ] string ) error {
2011-09-27 03:02:35 +00:00
if len ( args ) > 0 {
2011-09-26 00:40:01 +00:00
return ErrUsage
}
2011-01-18 02:28:38 +00:00
2011-04-02 05:26:33 +00:00
blobDir := path . Join ( osutil . CamliConfigDir ( ) , "keyblobs" )
os . Mkdir ( osutil . CamliConfigDir ( ) , 0700 )
2011-01-18 02:28:38 +00:00
os . Mkdir ( blobDir , 0700 )
2012-04-13 02:36:00 +00:00
keyId , err := c . keyId ( )
if err != nil {
return err
2011-01-18 02:28:38 +00:00
}
if os . Getenv ( "GPG_AGENT_INFO" ) == "" {
2012-04-13 02:36:00 +00:00
log . Printf ( "No GPG_AGENT_INFO found in environment; you should setup gnupg-agent. camput might be annoying otherwise, if your private key is encrypted." )
2011-01-18 02:28:38 +00:00
}
2012-04-13 02:36:00 +00:00
pubArmor , err := c . getPublicKeyArmored ( keyId )
2011-01-18 02:28:38 +00:00
if err != nil {
2012-04-13 02:36:00 +00:00
return err
2011-07-02 16:09:50 +00:00
}
2012-04-13 02:36:00 +00:00
bref := blobref . SHA1FromString ( string ( pubArmor ) )
2011-07-02 16:09:50 +00:00
keyBlobPath := path . Join ( blobDir , bref . String ( ) + ".camli" )
2012-04-13 02:36:00 +00:00
if err = ioutil . WriteFile ( keyBlobPath , pubArmor , 0644 ) ; err != nil {
2011-02-02 20:27:30 +00:00
log . Fatalf ( "Error writing public key blob to %q: %v" , keyBlobPath , err )
2011-01-18 02:28:38 +00:00
}
2011-07-02 16:09:50 +00:00
2011-01-18 02:28:38 +00:00
if ok , err := jsonsign . VerifyPublicKeyFile ( keyBlobPath , keyId ) ; ! ok {
2011-02-02 20:27:30 +00:00
log . Fatalf ( "Error verifying public key at %q: %v" , keyBlobPath , err )
2011-01-18 02:28:38 +00:00
}
2011-07-02 16:09:50 +00:00
2011-01-18 02:28:38 +00:00
log . Printf ( "Your Camlistore identity (your GPG public key's blobref) is: %s" , bref . String ( ) )
_ , err = os . Stat ( client . ConfigFilePath ( ) )
if err == nil {
2011-02-02 20:27:30 +00:00
log . Fatalf ( "Config file %q already exists; quitting without touching it." , client . ConfigFilePath ( ) )
2011-01-18 02:28:38 +00:00
}
2011-04-07 17:58:29 +00:00
if f , err := os . OpenFile ( client . ConfigFilePath ( ) , os . O_CREATE | os . O_EXCL | os . O_WRONLY , 0600 ) ; err == nil {
2011-01-18 02:28:38 +00:00
defer f . Close ( )
m := make ( map [ string ] interface { } )
2012-04-15 05:04:00 +00:00
m [ "keyId" ] = keyId // TODO(bradfitz): make this 'identity' to match server config?
2012-04-13 02:36:00 +00:00
m [ "publicKeyBlobref" ] = bref . String ( ) // TODO(bradfitz): not used anymore?
m [ "blobServer" ] = "http://localhost:3179/"
m [ "selfPubKeyDir" ] = blobDir
2012-11-16 15:49:19 +00:00
m [ "auth" ] = "localhost"
2011-01-18 18:29:38 +00:00
2011-01-18 02:28:38 +00:00
jsonBytes , err := json . MarshalIndent ( m , "" , " " )
if err != nil {
2011-02-02 20:27:30 +00:00
log . Fatalf ( "JSON serialization error: %v" , err )
2011-01-18 02:28:38 +00:00
}
_ , err = f . Write ( jsonBytes )
if err != nil {
2011-02-02 20:27:30 +00:00
log . Fatalf ( "Error writing to %q: %v" , client . ConfigFilePath ( ) , err )
2011-01-18 02:28:38 +00:00
}
log . Printf ( "Wrote %q; modify as necessary." , client . ConfigFilePath ( ) )
}
2011-09-26 00:40:01 +00:00
return nil
2011-02-02 20:27:30 +00:00
}