Esempio n. 1
0
// Token first tries to find a valid token in CacheFile, and otherwise uses
// Config and AuthCode to fetch a new token. This new token is saved in CacheFile
// (if not blank). If CacheFile did not provide a token and AuthCode is nil,
// ErrNoAuthCode is returned.
func (src TokenSource) Token() (*oauth2.Token, error) {
	var tok *oauth2.Token
	var err error
	if src.CacheFile != "" {
		tok, err = cachedToken(src.CacheFile)
		if err == nil {
			return tok, nil
		}
		if err != errExpiredToken {
			fmt.Printf("Error getting token from %s: %v\n", src.CacheFile, err)
		}
	}
	if src.AuthCode == nil {
		return nil, ErrNoAuthCode
	}
	tok, err = src.Config.Exchange(oauth2.NoContext, src.AuthCode())
	if err != nil {
		return nil, fmt.Errorf("could not exchange auth code for a token: %v", err)
	}
	if src.CacheFile == "" {
		return tok, nil
	}
	tokenData, err := json.Marshal(&tok)
	if err != nil {
		return nil, fmt.Errorf("could not encode token as json: %v", err)
	}
	if err := wkfs.WriteFile(src.CacheFile, tokenData, 0600); err != nil {
		return nil, fmt.Errorf("could not cache token in %v: %v", src.CacheFile, err)
	}
	return tok, nil
}
func writeDefaultConfigFile() error {
	if err := wkfs.MkdirAll(baseConfig.BlobPath, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	if err := wkfs.MkdirAll(path.Base(camliServerConf), 0700); err != nil {
		return fmt.Errorf("Could not create default config directory: %v", err)
	}

	keyID, err := getOrMakeKeyring()
	if err != nil {
		return err
	}
	baseConfig.Identity = keyID
	baseConfig.Auth = fmt.Sprintf("userpass:%s:%s", *flagUsername, *flagPassword)

	confData, err := json.MarshalIndent(baseConfig, "", "    ")
	if err != nil {
		return fmt.Errorf("Could not json encode config file : %v", err)
	}
	if err := wkfs.WriteFile(camliServerConf, confData, 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}

	// chown everything back to "camli" since we run as root.
	// TODO(mpl): do it in Go with os.Chown + filepath.Walk (for recursion), if we care.
	if out, err := exec.Command("chown", "-R", camliUsername+":"+camliUsername, path.Join(home, "var")).CombinedOutput(); err != nil {
		return fmt.Errorf("chown error: %v, %v", string(out), err)
	}
	if out, err := exec.Command("chown", "-R", camliUsername+":"+camliUsername, path.Join(home, ".config")).CombinedOutput(); err != nil {
		return fmt.Errorf("chown error: %v, %v", string(out), err)
	}
	return nil
}
Esempio n. 3
0
// WriteDefaultConfigFile generates a new default high-level server configuration
// file at filePath. If useSQLite, the default indexer will use SQLite, otherwise
// leveldb. If filePath already exists, it is overwritten.
func WriteDefaultConfigFile(filePath string, useSQLite bool) error {
	conf := defaultBaseConfig
	blobDir := osutil.CamliBlobRoot()
	if err := wkfs.MkdirAll(blobDir, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	conf.BlobPath = blobDir
	if useSQLite {
		conf.SQLite = filepath.Join(osutil.CamliVarDir(), "index.sqlite")
	} else {
		conf.LevelDB = filepath.Join(osutil.CamliVarDir(), "index.leveldb")
	}

	keyID, secretRing, err := getOrMakeKeyring()
	if err != nil {
		return err
	}
	conf.Identity = keyID
	conf.IdentitySecretRing = secretRing

	confData, err := json.MarshalIndent(conf, "", "    ")
	if err != nil {
		return fmt.Errorf("Could not json encode config file : %v", err)
	}

	if err := wkfs.WriteFile(filePath, confData, 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}

	return nil
}