Exemple #1
0
func Initialize(listenAddress string) {

	router = mux.NewRouter()
	router.NotFoundHandler = http.HandlerFunc(notFoundHandler)

	go func() {
		log.Printf(log.Info, "Starting HTTP server on address: [%s]", listenAddress)
		e := http.ListenAndServe(listenAddress, router)
		if e != nil {
			log.Printf(log.Error, "Error starting HTTP server: %s", e.Error())
		}
	}()
}
Exemple #2
0
func Decrypt(key, text []byte) []byte {
	block, err := aes.NewCipher(key)
	if err != nil {
		log.Printf(log.Error, "Could not create new cipher using key: "+err.Error())
		return nil
	}
	if len(text) < aes.BlockSize {
		log.Printf(log.Error, "Encrypted text was too short for the blocksize used.")
		return nil
	}
	iv := text[:aes.BlockSize]
	text = text[aes.BlockSize:]
	cfb := cipher.NewCFBDecrypter(block, iv)
	cfb.XORKeyStream(text, text)
	return decodeBase64(text)
}
Exemple #3
0
func Encrypt(key, text []byte) []byte {
	block, err := aes.NewCipher(key)
	if err != nil {
		log.Printf(log.Error, "Could not create new cipher using key: "+err.Error())
		return nil
	}
	b := encodeBase64(text)
	ciphertext := make([]byte, aes.BlockSize+len(b))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		log.Printf(log.Error, "Could not get random string: "+err.Error())
		return nil
	}
	cfb := cipher.NewCFBEncrypter(block, iv)
	cfb.XORKeyStream(ciphertext[aes.BlockSize:], b)
	return ciphertext
}
Exemple #4
0
func memProfileSaver(minuteInterval int, fileName string) {
	i := 1
	for {
		select {
		case <-time.After(time.Duration(minuteInterval) * time.Minute):
			f, err := os.Create(fileName + fmt.Sprintf(".%d", i*minuteInterval))
			if err != nil {
				log.Printf(log.Warn, "Could not write memory profile: "+err.Error())
				break
			}
			pprof.WriteHeapProfile(f)
			log.Printf(log.Info, "Saved memory profile.")
			i++
			f.Close()
			break
		}
	}
}
Exemple #5
0
func AppendToCsv(fileName string, fields []string) {

	fh, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0660)
	if err != nil {
		fh, err = os.OpenFile(fileName, os.O_RDWR|os.O_APPEND, 0660)
	}
	if err != nil {
		log.Printf(log.Warn, "Could not open file: "+err.Error())
		return
	}
	defer func() {
		fh.Sync()
		fh.Close()
	}()
	w := csv.NewWriter(fh)
	err = w.Write(fields)
	if err != nil {
		log.Printf(log.Warn, "Could not write file: "+err.Error())
		return
	}
	w.Flush()
}
Exemple #6
0
func (d *DynamoDb) CreateTable(tableName string, hashKey *AttributeDef, rangeKey *AttributeDef, readCapacity int, writeCapacity int) error {

	body := "{"

	//table name
	body += fmt.Sprintf("\"TableName\":\"%s\",", tableName)

	//attributes
	body += "\"AttributeDefinitions\": ["

	body += fmt.Sprintf("{\"AttributeName\":\"%s\",\"AttributeType\":\"%s\"}", hashKey.Name, hashKey.Type)

	if rangeKey != nil {
		body += fmt.Sprintf("{\"AttributeName\":\"%s\",\"AttributeType\":\"%s\"}", rangeKey.Name, rangeKey.Type)
	}

	body += fmt.Sprintf("], ")

	//keys
	body += "\"KeySchema\": ["

	body += fmt.Sprintf("{\"AttributeName\":\"%s\",\"KeyType\":\"%s\"}", hashKey.Name, "HASH")

	if rangeKey != nil {
		body += fmt.Sprintf(",{\"AttributeName\":\"%s\",\"KeyType\":\"%s\"}", rangeKey.Name, "RANGE")
	}
	body += fmt.Sprintf("], ")

	body += fmt.Sprintf("\"ProvisionedThroughput\": { \"ReadCapacityUnits\": %d, \"WriteCapacityUnits\": %d }", readCapacity, writeCapacity)

	body += "}"

	ret, err := d.Execute("DynamoDB_20120810.CreateTable", body)
	if err != nil {
		return err
	}

	var t struct {
		TableDescription struct {
			TableName   string
			TableStatus string
		}
	}
	err = json.Unmarshal([]byte(ret), &t)
	if err != nil {
		return err
	}

	log.Printf(log.Warn, "Table Status: "+t.TableDescription.TableStatus)
	return nil
}
Exemple #7
0
func ConfigureProfiling(config string) {

	s := strings.Split(config, ",")
	if len(s) >= 2 {
		switch s[0] {
		case "cpu", "CPU":
			activeCpuProfiling = true
			f, err := os.Create(s[1])
			if err != nil {
				log.Printf(log.Warn, "Could not initialize CPU profiler: "+err.Error())
				break
			}
			pprof.StartCPUProfile(f)
			break
		case "mem", "MEM":
			activeMemProfiling = true
			interval := 10
			if len(s) == 3 {
				v, err := strconv.Atoi(s[2])
				if err == nil {
					interval = v
				}
			}
			go memProfileSaver(interval, s[1])
			break
		case "web", "WEB":
			activeWebProfiling = true
			go func() {
				err := http.ListenAndServe(s[1], nil)
				if err != nil {
					log.Printf(log.Warn, "Could not initialize debug web server: "+err.Error())
				}
			}()
			break
		}
	}
}
Exemple #8
0
func (d *DynamoDb) Query(so *QueryRequest) (*QueryResponse, error) {

	if len(so.TableName) == 0 {
		return nil, aws.ErrArguments
	}

	body := "{"
	body += "\"TableName\":\"" + so.TableName + "\","
	if len(so.IndexName) > 0 {
		body += "\"IndexName\":\"" + so.IndexName + "\","
	}
	if so.Limit > 0 {
		body += fmt.Sprintf("\"Limit\":%d,", so.Limit)
	}
	if len(so.Select) > 0 {
		body += fmt.Sprintf("\"Select\":\"%s\",", so.Select)
	}
	if so.keyConditions != nil && len(so.keyConditions) > 0 {
		body += "\"KeyConditions\":{"
		i := 0
		for k, v := range so.keyConditions {
			if i > 0 {
				body += ","
			}
			if v.Attr2 != nil {
				body += fmt.Sprintf("\"%s\":{\"AttributeValueList\":[%s,%s],\"ComparisonOperator\":\"%s\"}", k, v.Attr1.ToJson(), v.Attr2.ToJson(), v.Comparison)
			} else {
				body += fmt.Sprintf("\"%s\":{\"AttributeValueList\":[%s],\"ComparisonOperator\":\"%s\"}", k, v.Attr1.ToJson(), v.Comparison)
			}
			i++
		}
		body += "},"
	}

	if so.attributesToGet != nil && len(so.attributesToGet) > 0 {
		body += "\"AttributesToGet\":["
		i := 0
		for _, v := range so.attributesToGet {
			if i > 0 {
				body += ","
			}
			body += fmt.Sprintf("\"%s\"", v)
			i++
		}
		body += "],"
	}
	body = strings.TrimRight(body, ",")
	body += "}"

	if aws.DebugMode {
		log.Printf(log.Trace, "Request: "+body)
	}

	ret, err := d.Execute("DynamoDB_20120810.Query", body)
	if err != nil {
		return nil, err
	}

	if aws.DebugMode {
		log.Printf(log.Trace, "Response: "+ret)
	}
	//var t interface{}
	//err = json.Unmarshal([]byte(ret), &t)
	//fmt.Printf("%s\n\n",ret)
	//fmt.Printf("%#v\n\n",t)

	type JsonQueryResponse struct {
		Count int
		Items []map[string]jsonValue
	}

	var jresp JsonQueryResponse
	err = json.Unmarshal([]byte(ret), &jresp)

	resp := new(QueryResponse)
	resp.Count = jresp.Count
	resp.Items = make([]map[string]*AttributeValue, resp.Count)
	for k, v := range jresp.Items {
		m := make(map[string]*AttributeValue)

		jsonGetValues(&v, &m)

		resp.Items[k] = m
	}
	return resp, nil
}
Exemple #9
0
func (d *DynamoDb) Scan(so *ScanRequest) (*ScanResponse, error) {

	if len(so.TableName) == 0 {
		return nil, aws.ErrArguments
	}

	body := "{"
	body += "\"TableName\":\"" + so.TableName + "\","
	if so.Limit > 0 {
		body += fmt.Sprintf("\"Limit\":%d,", so.Limit)
	}
	if so.ScanFilter != nil && len(so.ScanFilter) > 0 {
		body += "\"ScanFilter\":{"
		i := 0
		for k, v := range so.ScanFilter {
			if i > 0 {
				body += ","
			}
			body += fmt.Sprintf("\"%s\":{\"AttributeValueList\":[%s],\"ComparisonOperator\":\"%s\"}", k, v.Attr.ToJson(), v.Comparison)
			i++
		}
		body += "},"
	}

	if !so.StartHashKey.IsEmpty() {
		body += "\"ExclusiveStartKey\":{"
		body += fmt.Sprintf("\"HashKeyElement\":%s", so.StartHashKey.ToJson())
		if len(so.StartRangeKey.Value) > 0 {
			body += fmt.Sprintf(",\"RangeKeyElement\":%s", so.StartRangeKey.ToJson())
		}
		body += "},"
	}

	if so.AttributesToGet != nil && len(so.AttributesToGet) > 0 {
		body += "\"AttributesToGet\":["
		i := 0
		for _, v := range so.AttributesToGet {
			if i > 0 {
				body += ","
			}
			body += fmt.Sprintf("\"%s\"", v)
			i++
		}
		body += "],"
	}
	body = strings.TrimRight(body, ",")
	body += "}"

	if aws.DebugMode {
		log.Printf(log.Trace, "Request: "+body)
	}

	ret, err := d.Execute("DynamoDB_20111205.Scan", body)
	if err != nil {
		return nil, err
	}

	if aws.DebugMode {
		log.Printf(log.Trace, "Response: "+ret)
	}
	//var t interface{}
	//err = json.Unmarshal([]byte(ret), &t)
	//fmt.Printf("%s\n\n",ret)
	//fmt.Printf("%#v\n\n",t)

	type JsonScanResponse struct {
		Count        int
		ScannedCount int
		Items        []map[string]jsonValue
	}

	var jresp JsonScanResponse
	err = json.Unmarshal([]byte(ret), &jresp)

	resp := new(ScanResponse)
	resp.Count = jresp.Count
	resp.ScannedCount = jresp.ScannedCount
	resp.Items = make([]map[string]*AttributeValue, resp.Count)
	for k, v := range jresp.Items {
		m := make(map[string]*AttributeValue)

		jsonGetValues(&v, &m)

		resp.Items[k] = m
	}
	return resp, nil
}
Exemple #10
0
func (d *DynamoDb) GetItem(req *GetItemRequest) (*GetItemResponse, error) {

	if len(req.TableName) == 0 {
		return nil, aws.ErrArguments
	}

	body := "{"
	body += "\"TableName\":\"" + req.TableName + "\","

	body += "\"Key\":{"
	if req.HashKey != nil {
		body += req.HashKey.ToJson()
	}
	if req.RangeKey != nil {
		body += "," + req.RangeKey.ToJson()
	}
	body += "},"

	if req.attributesToGet != nil && len(req.attributesToGet) > 0 {
		body += "\"AttributesToGet\":["
		i := 0
		for _, v := range req.attributesToGet {
			if i > 0 {
				body += ","
			}
			body += fmt.Sprintf("\"%s\"", v)
			i++
		}
		body += "],"
	}
	body = strings.TrimRight(body, ",")
	body += "}"

	if aws.DebugMode {
		log.Printf(log.Trace, "Request: "+body)
	}

	ret, err := d.Execute("DynamoDB_20120810.GetItem", body)
	if err != nil {
		return nil, err
	}

	if aws.DebugMode {
		log.Printf(log.Trace, "Response: "+ret)
	}
	//var t interface{}
	//err = json.Unmarshal([]byte(ret), &t)
	//fmt.Printf("%s\n\n",ret)
	//fmt.Printf("%#v\n\n",t)

	type JsonGetItemResponse struct {
		Item map[string]jsonValue
	}

	var jresp JsonGetItemResponse
	err = json.Unmarshal([]byte(ret), &jresp)

	resp := new(GetItemResponse)
	resp.Attributes = make(map[string]*AttributeValue)
	jsonGetValues(&jresp.Item, &resp.Attributes)
	return resp, nil
}