Skip to content

Commit

Permalink
Merge pull request #88 from AirHelp/add-writefile-command
Browse files Browse the repository at this point in the history
new feauture: write file content into treasury key
  • Loading branch information
jazzbee authored Nov 6, 2019
2 parents 1a69b4f + 8e792af commit 080ed41
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ Success! Data written to: development/webapp/cockpit_api_pass

Note: if secret value is equal to existing one, write is skipped. `--force` flag can be used to overwrite.

### Write file content
```
> treasury write development/webapp/credentials_yaml <filename> --file
Success! Data written to: development/webapp/credentials_yaml
```

Note: if secret value is equal to existing one, write is skipped. `--force` flag can be used to overwrite. Stored file content in key is gzipped and converted to base64. You can decode it with bash script easily:
```
> treasury read development/webapp/credentials_yaml | base64 -D | gzip -d > creadentails.yaml
```
or you can handle it with your application. Size limit depends on backend that you are using - for AWS SSM size limit is 4096 bytes (after gzip and base64).


### Read secret
```
> treasury read development/webapp/cockpit_api_pass
Expand Down
24 changes: 24 additions & 0 deletions client/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"github.com/AirHelp/treasury/types"
"github.com/AirHelp/treasury/utils"
"github.com/aws/aws-sdk-go/aws/awserr"

"bytes"
"compress/gzip"
b64 "encoding/base64"
"io/ioutil"
)

const (
Expand Down Expand Up @@ -48,3 +53,22 @@ func (c *Client) Write(key, secret string, force bool) error {
}
return nil
}

func (c *Client) WriteFile(key, file string, force bool) error {

data, err := ioutil.ReadFile(file)
if err != nil {
return err
}

var gzipped bytes.Buffer
gz := gzip.NewWriter(&gzipped)
if _, err := gz.Write(data); err != nil {
return err
}
if err := gz.Close(); err != nil {
return err
}

return c.Write(key, b64.StdEncoding.EncodeToString(gzipped.Bytes()), force)
}
15 changes: 14 additions & 1 deletion cmd/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func init() {
RootCmd.AddCommand(writeCmd)
writeCmd.SuggestFor = []string{"put"}
writeCmd.PersistentFlags().Bool("force", false, "Force overwrite secret value")
writeCmd.PersistentFlags().Bool("file", false, "Save file content into Treasury")
}

func write(cmd *cobra.Command, args []string) error {
Expand All @@ -47,17 +48,29 @@ func write(cmd *cobra.Command, args []string) error {
return err
}

file, err := cmd.Flags().GetBool("file")
if err != nil {
return err
}

treasury, err := client.New(&client.Options{
Region: s3Region,
S3BucketName: treasuryS3,
})
if err != nil {
return err
}
err = treasury.Write(key, value, force)

if file {
err = treasury.WriteFile(key, value, force)
} else {
err = treasury.Write(key, value, force)
}

if err != nil {
return err
}

fmt.Println("Success! Data written to: ", key)
return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ require (
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 // indirect
)

go 1.13
14 changes: 13 additions & 1 deletion test/bats/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,25 @@ invalid_aws_region=us-west-1
[ $status -eq 0 ]
}


@test "template wrong key" {
run $treasury template --src test/resources/bats-wrong-source.secret.tpl --dst test/output/bats-output.secret
[ $status -eq 255 ]
[[ ${lines[0]} =~ "Error" ]]
}

@test "write file content to treasury key" {
run $treasury write development/treasury/key5 test/resources/test_file --file
[ $status -eq 0 ]
run $treasury read development/treasury/key5
[[ ${lines[0]} =~ "H4sIAAAAAAAA/yopSk0sLi2q5OICBAAA///FZR9LCgAAAA==" ]]
}

@test "write too large file content to treasury key" {
run $treasury write development/treasury/key5 test/resources/test_large_file --file
[ $status -eq 255 ]
[[ ${lines[0]} =~ "Error" ]]
}

@test "check version" {
run $treasury version
[ $status -eq 0 ]
Expand Down
2 changes: 2 additions & 0 deletions test/resources/test_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
treasury

1 change: 1 addition & 0 deletions test/resources/test_large_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eec4geig0too2iethaef6ohNgoor5Joqu6ahdai3iec0chahbielaivae4gohsooHel4EiChee6aif8If6aish5gae6chah5iesh1nae5erahkai6oo1kiu5aeso6kahph3eaCeu3ey0Thoo9eeco6chevao0aiVoo9za3Ma2Ath4baewa0sheaph7Quae8iz0AetaeshoRoo6ohthai7mai1azai5hea0Aep9Oogod4eifahsaiQueidoh0kae2fi2Vohkah5faiP9Ooghuumaing6aechoipho7yipheBuughahphue5aequ8ooj0eeChooyaiquea8ahlaefaic9ooMevaqu5oth9aiWai8eic9ieb7mohshooLoo2sheeCh2zee1deiqu1Shokechooqu7zie9ooLi6nosh2uiNeichag4eixe3aecuu7boogeuQuouWeesheinahj9nohuef4thoosoo0eil5oofohp4aeco8she2uLequaesohz6shei1je2Mahdo0lohhohyoh0RieNgi3ahhaewoquaing8xohsh9eniegae4bei3oor3eeCeeb3aeghu2ahchovohc2cooXo2kaequieshohngoepe0chaiwieGux4juo9aibaingei2Ohho4chaiT9sooM9lahba8peeche8Ien2aingue0oopuZ6ielu6zaiwoopha0AbeYeneeta3oovuach1XohSh0ij5ohtheaqu8tutubeebi9EiLee9pa5AetaizaeK7tha6zaengiepohbohWeshaek1uGheedeiFeipuBoch6eegu8toozeed0aigogh4thooleinevaiphovei3rongee6eirinofieroovelangah2Gecaepaqueiqu8ookey1yee4aiphei8aeXei0aH4Aiqueigh5eileepaaS0lei7oopheetaeRiv4rubai3waiveGei0oj3Zohlee8hee8guon7aef1ief8quae8ahpheec2ooM4fee6IshoodohdohKeethoye1fo5ichu5thae5sahpuLau9uiThei0aejie4aimoo6Yohza0wo8yiej2jeep1wehuKui2ziekaemaiph5noh2she0cai7deigiphai4eel2woisood4pemaeBe6iequ8Aewiequimaiqu6QueiJ2lohh1vuaWairohLee7caigheiphaeg5Jae8nae9booseiphui2aijeiqui9ieceiFud6uu9eiP5eikiechaif3chee2UutooleegubiewaeTheigahrue2eM7pai2xiodi0quahqua9neeZaeloh5qua8EingiecaPhephohxie5Lee2taivohx0ahl4aeNgoo0yieng4Raizu4poosh8acoh7Ebae2ahyah7aikaiTh9heequ8keughahFie3ohloHae6sheiyu3aeji7too2aecuphei8sheuZ7ox2liw4hai6eezaicheephiePheichosaph7rei4ing1je7Aesheehocoh4ud5Eirudohpuree4Uo1Baingaash1aetaen1AiV3teozieth0aipiihoodofuephaenaesahjohxungikooDoopiehuch4shuiDaThie9ait8GiQuush4uu4geiNgoo0eithohg0iephengaeb5eelee3fu1chieneingie6Kee9oobaeHuh9wozie5Iem8isea4eew9ahhefee1ua8achic9Geceequeegahha9xah8Vu4jilur1Thiwaeyiesaij3joo0uu1ohch5uePoo7azai3wei5eeJah0AdirohmuuNgoiCab2eipuo4choopheengiegho2Eath3Ang7aiShoath7tie5ZooW8yodee1Cha0eiTheeh5thaolohtee0ia9uf6yaexeepheen8fi5aLaet1chooyahleeHah4aRae1yie2Xa0oquojah9uawag8eejuuruvei6ies1Aehoo2aeji1uo6yeekei1mohchai0phiije5eip4wi4rauyooy9ayooph6laga5gothio2mie8IekooChahyohkaebie1eejufohseesah7uzaePhoon8kahdie2then2rejaipheez6koobepa1Eer8TeechePh0ieMevoo0taenguvai5quoo9eshu8oR0Daejii2atheing0oe9see5phoo3vooLahshoovieQu0Lu2ush6cuuph6ko5Eih3jun7Choo8och7ohMeengaiqu9loojoquiT0ji1eeweecaegootaef7xaem8Aif8Ih5zohhoo5eviereev0aicoo0eev5Ielaithee4yee7EevuaSaequ9Gee8TiVeimieyulo1ohCh6ieheeshait0niseiWaigheigaihaiP3ahta1ohbae7feesicov2re1Ohc4ip5ofah5tha0kai9iigezoobi5ba3Tio6veengiedooqu4ooz3phae4meinahPh9ighaeLeij4cesoo0eeshinguaCeeShong4she0ae8quoh8opaocogh4soe1aihaethienaemae3aeh5tiegooz9Phoo1iQu6tei9easoo2phaing1oop1meo2BahgooN0hooFai0EeleejooBaeho3eechifoge8aoZahghohr0iekohGh8ahle6PieShoh8auwoo7thei4iehing4se0cee2joBu0vaephae7Ahthu4Piephai8dooN1Te6phiekai2deireijochaingeinaelaek4biatail1OolieR3Iequ9eiLeithoogee4eiHah7uiQu2Aya9akoof4thieyif5Ohn6ai2oaDei5pieph6eep2oZooD9xeighochoo3jou8ui6ouS0dohvaicu8Ohjahmahhai8AhChiulei9bie0oPair4eeFieheJeshohchaizeiyoi0Theich2iiF0jae7Neizei7reu6Nootah2ii7ou4XaiG8Eep6oimau5aiwaez9aeh4meeboosh6auz2adeighe9oiYeeciekoo6AiFoo8ahSeedie9ief8oiboC8Ow5Iqu5af9fu9ahThoi1ailobieleeDoa8ohc4iV1Eelucana8FiuThaiLaeD0FauKiepaiv1ia9oiquaenepaiB1kooboh4yoje9phup5ongiV6doh6oohaey7ji4taengahy2Huja5maishoje6to5ax2cheugooy0aiShaeghie1osh4Ohmeidied0aequaiphe6thie5aevang6kei4shaiS6ohgha9og5ni1mo9Looz6angongai8aeghiXae8OoheiL0googh3eecela9caeThoy0miob2aiw7xuwedoot9joh6eibe2woo9beiv1Uivoh8gohw9ahsiezeiv2uWahguu8ieDahghaing1Uwooshun6mie0ooXeghoof9Yee1ahTah2uChu0oNe5aet2DeeHeech2xo3xu9Ahp0Ooc8de6Sae6lae4lon0nohNgaazohm2PohPheth3Reex7vahpheinioveimid8eephee2ohkushaer2aiyieng8bopoote1coogei4ahquei1aikaigh1ieghienek0jai4fooThaeph4le6ughoh6pheoshaewahD5peid0equ9ainee2noohojeel0boh8kaijeePieciedah2Bahngahp4ahquail9queugoo1ahngeingaivait2rixa6Lohv6phac7duG7xu9aeshie9oe0hucooNei1wei8fi1aedaNoo3zohmeTiupeejo1quohmah9ooZeeVochuP3Xee6loo5cieMiegh8go7zahceeT3Wah1ahca8Sah3eequoo0thaiDae7Shi3Eedeu2exoJaehaipuphubahveepe4deive6Lahci0uer4iC1zaifequeurioshiesh6joohi4oDeeGai0shivieleizei3lahzik9lohp7sheiDathoo6iZ5rahPhue6EMuZeedeiyeiyaidoocheesei6hoolei0upoo9kuaqu2Eo7eeyoomah5oleiLieth7reer9Layahtai4Oonie7eijaiPhohPief1cahbe7ahsh2ooyaigeechieph1eih9wohKohphitah6oos8naethohph9ohX4thie0aichi6aithaighai8aeWeesohX7ke3ohH6alemahShoch5iKas8ohhogaep8eayo9aegu2Queivooshahlo7ao9Sahj0ueMoh2viuhou3zaecobah8pei5shohphohdoo9aiT9ooxixiegaNgei2Diethahv7Ohngoh8ho5AghaeB5quaet0wicohphahd6ohSe4ieNei0phidoozeeYae5eto2phajaipie0EizoeVooqu6AeCahgaQuag1Fai6Coo9it7iexofohQuee1eijahxaok0Bie7quei5Ooj9quiesahy7heephahceth9si7on0ohthishowah7Iiwi8Weeh8jeibeid0xu9ceifiaviutiej8aifi5eishaethahtebemeije0ekue4ohsookexohCh6eRohCh4xae4ohpootoosouxo9keitahzang5queegahTiaPeefieshieCoe3deepai6OhxuThegh7aeshaeziu0queixa5iongaiGeoxoosha9Jah7oesoa3Lochee6theeYakahdoo0iequaxei5jah8meixahmoeJ4ahfeetaeph1aez2quu7shah5mi1eev8uSh8Yoshai3aih7oineiphahngoh5uy2eiB6Wohpah8Zoothae3meS4Le0Aiweesah7sheeThucae5Eigh6kuokooTh7Uu0eingohfieng9zah9WuKev7Xi4Woe1she0Eeth0PhawoaGh3eid8neel2choo6Ohz8aegaiphooyiethohb7boh8sae2nie4xai9ahSeereilahnuJooyereepibee2EcheiJah7iiz8Ei9eath0ieXoo7vuzoo9yiequahdohba6im5iugheeNoh0aej4shaevoo1reeSh1ga0xu2shieF2rahqu4tai4phieb8ohsh7ahthohqueem9xahghaiheik3ShuR3xor5Eenahr8oomeTiveiChahk5chul5ohfajeeH9zaiph3ien2cahxu9Ahkie6saV4geth9oohah9xieZuK7aaGhi6ohboon2shaHuGh2Ielaiw0aeshoozooloongioraeNeev3wooFaighav4iom6uo9bah4Roh7tahrohcailaing2Aituxeix5Daiw8ahLoon3xa0pie2KooDu8SooNgaefeic1aif7ui7ohj8suo7EeZ8ahX2naew9AiNga7ro2hei1Ohp5eim4mahn5aeh5Chaith4abeechio9ceesoh5Theej8wahghi7aishecaeL0Phi1Phaey2ot9ohco8Kielohteno0eeghoht9rooc8se3kuw3ein8iexa2Geesahcaereev6dea6ohg1reiHaeQuohmah1aifahtuaraequau9ooNgiuheaw4ciCh7Du6doodied3Peno0AhmegahWaaTaibaicixef3Ha2ap5Kieheeve9Epiroh4ohWua7shaupeireiPaer9le9thaeHuShaimaiXahy6aiGheeX5ix5lo1iTai2Xoopize5eiZ4uu5iupa2iefohnei5ieceechaerah8Ido9du4eicaamaejeequeexahlo3geesahgheicheim9Yeengoghae3biko7mi1Bee2ohc5Thaequaekeegh7coo6eeR4shai0ooch5zae6thahyeido1uix8ve1mee6iR7ohxoajaeDoi7eegoong7iemee0ohwau1la2Aenge6ele
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// treasury version should be changed here
const version = "0.5.2"
const version = "0.6.0"

// This will be filled in by the compiler.
var (
Expand Down

0 comments on commit 080ed41

Please sign in to comment.