func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (bs blobserver.Storage, err error) { sto := &storage{ SimpleBlobHubPartitionMap: &blobserver.SimpleBlobHubPartitionMap{}, index: index.NewMemoryStorage(), // TODO: temporary for development; let be configurable (mysql, etc) } agreement := config.OptionalString("I_AGREE", "") const wantAgreement = "that encryption support hasn't been peer-reviewed, isn't finished, and its format might change." if agreement != wantAgreement { return nil, errors.New("Use of the 'encrypt' target without the proper I_AGREE value.") } key := config.OptionalString("key", "") keyFile := config.OptionalString("keyFile", "") var keyb []byte switch { case key != "": keyb, err = hex.DecodeString(key) if err != nil || len(keyb) != 16 { return nil, fmt.Errorf("The 'key' parameter must be 16 bytes of 32 hex digits. (currently fixed at AES-128)") } case keyFile != "": // TODO: check that keyFile's unix permissions aren't too permissive. keyb, err = ioutil.ReadFile(keyFile) if err != nil { return nil, fmt.Errorf("Reading key file %v: %v", keyFile, err) } } blobStorage := config.RequiredString("blobs") metaStorage := config.RequiredString("meta") if err := config.Validate(); err != nil { return nil, err } sto.blobs, err = ld.GetStorage(blobStorage) if err != nil { return } sto.meta, err = ld.GetStorage(metaStorage) if err != nil { return } if keyb == nil { // TODO: add a way to prompt from stdin on start? or keychain support? return nil, errors.New("no encryption key set with 'key' or 'keyFile'") } if err := sto.setKey(keyb); err != nil { return nil, err } log.Printf("Reading encryption metadata...") if err := sto.readAllMetaBlobs(); err != nil { return nil, fmt.Errorf("Error scanning metadata on start-up: %v", err) } log.Printf("Read all encryption metadata.") return sto, nil }
func newTestStorage() *testStorage { sto := &storage{ index: index.NewMemoryStorage(), } if err := sto.setKey(testKey); err != nil { panic(err) } ts := &testStorage{ sto: sto, blobs: new(test.Fetcher), meta: new(test.Fetcher), } sto.blobs = ts.blobs sto.meta = ts.meta sto.testRandIV = func() []byte { ts.mu.Lock() defer ts.mu.Unlock() var ret [16]byte ts.iv++ binary.BigEndian.PutUint64(ret[8:], ts.iv) return ret[:] } return ts }