func playWithSecureStorage(storage *ss.SecureStorage, secret []byte) {
	fileName := "try.txt"
	defer os.Remove(fileName)
	err := storage.StoreInfo(fileName)
	if err != nil {
		fmt.Println("Error while saving:", err)
	}

	fmt.Println("Original data:")
	fmt.Println(storage.GetDecryptStorageData())

	keyText := fmt.Sprintf(keyFmt, 1)
	err = storage.RemoveItem(keyText)
	if err != nil {
		fmt.Println("Error while remove key:", err)
	}

	fmt.Println("After removing:")
	fmt.Println(storage.GetDecryptStorageData())

	sd, err := ss.LoadInfo(fileName, secret)
	if err != nil {
		fmt.Println("Error while reading:", err)
	}
	fmt.Println("The data that was read from file:", fileName)
	fmt.Println(sd.GetDecryptStorageData())
}
示例#2
0
// Load the EntityManager data from the storage
// and constract/reconstract the EntityManager
func LoadInfo(filePath string, secret []byte, el *EntityManager) error {
	prefix := ""
	if el == nil {
		return fmt.Errorf("internal error: Entity list is nil")
	}
	stStorage, err := ss.LoadInfo(filePath, secret)
	if err != nil {
		logger.Error.Printf("%v", err)
		return fmt.Errorf("%v", err)
	}
	storage := stStorage.GetDecryptStorageData()
	if storage == nil {
		return fmt.Errorf("loadInfo: Storage is nil")
	}
	for key, value := range storage.Data {
		userType := strings.HasPrefix(key, getEntityStoreFmt(userTypeStr+prefix, entityToken, ""))
		groupType := strings.HasPrefix(key, getEntityStoreFmt(groupTypeStr+prefix, entityToken, ""))
		resourceType := strings.HasPrefix(key, getEntityStoreFmt(resourceTypeStr+prefix, entityToken, ""))
		var err error
		var name string
		var e *Entity
		var g *Group
		if userType {
			e, err = readEntityFromStorage(key, storage)
			name = e.Name
			el.Users[name] = &User{Entity: *e}
		} else if groupType {
			g, err = readGroupFromStorage(key, storage)
			name = g.Name
			el.Groups[name] = g
		} else if resourceType {
			e, err = readEntityFromStorage(key, storage)
			name = e.Name
			el.Resources[name] = &(Resource{Entity: *e})
		}
		if err != nil {
			return fmt.Errorf("while reading file: '%s', string: '%s', error: %s", filePath, value, err)
		}
		// fmt.Println("key:", key, "Value:", value, "error:", err)
		if userType || groupType || resourceType {
			for propertyName, property := range stc.Serializers {
				data, err := property.ReadFromStorage(getPropertyStoreFmt(propertyName, name), storage)
				if err == nil { // the item exist for this entity
					err = el.AddPropertyToEntity(name, propertyName, data)
					if err != nil {
						fmt.Println("while reading property data", propertyName, "for entity", name, "error:", err)
						return err
					}
				}
			}
		}
	}
	return nil
}