func PostRepo(c *gin.Context) { remote := remote.FromContext(c) user := session.User(c) owner := c.Param("owner") name := c.Param("name") if user == nil { c.AbortWithStatus(403) return } r, err := remote.Repo(user, owner, name) if err != nil { c.String(404, err.Error()) return } m, err := remote.Perm(user, owner, name) if err != nil { c.String(404, err.Error()) return } if !m.Admin { c.String(403, "Administrative access is required.") return } // error if the repository already exists _, err = store.GetRepoOwnerName(c, owner, name) if err == nil { c.String(409, "Repository already exists.") return } // set the repository owner to the // currently authenticated user. r.UserID = user.ID r.AllowPush = true r.AllowPull = true r.Timeout = 60 // 1 hour default build time r.Hash = crypto.Rand() // crates the jwt token used to verify the repository t := token.New(token.HookToken, r.FullName) sig, err := t.Sign(r.Hash) if err != nil { c.String(500, err.Error()) return } link := fmt.Sprintf( "%s/hook?access_token=%s", httputil.GetURL(c.Request), sig, ) // generate an RSA key and add to the repo key, err := crypto.GeneratePrivateKey() if err != nil { c.String(500, err.Error()) return } keys := new(model.Key) keys.Public = string(crypto.MarshalPublicKey(&key.PublicKey)) keys.Private = string(crypto.MarshalPrivateKey(key)) // activate the repository before we make any // local changes to the database. err = remote.Activate(user, r, keys, link) if err != nil { c.String(500, err.Error()) return } // persist the repository err = store.CreateRepo(c, r) if err != nil { c.String(500, err.Error()) return } keys.RepoID = r.ID err = store.CreateKey(c, keys) if err != nil { c.String(500, err.Error()) return } c.JSON(200, r) }
func Test_keystore(t *testing.T) { db := openTest() defer db.Close() s := From(db) g := goblin.Goblin(t) g.Describe("Keys", func() { // before each test be sure to purge the package // table data from the database. g.BeforeEach(func() { db.Exec(rebind("DELETE FROM `keys`")) }) g.It("Should create a key", func() { key := model.Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err := s.Keys().Create(&key) g.Assert(err == nil).IsTrue() g.Assert(key.ID != 0).IsTrue() }) g.It("Should update a key", func() { key := model.Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err := s.Keys().Create(&key) g.Assert(err == nil).IsTrue() g.Assert(key.ID != 0).IsTrue() key.Private = "" key.Public = "" err1 := s.Keys().Update(&key) getkey, err2 := s.Keys().Get(&model.Repo{ID: 1}) g.Assert(err1 == nil).IsTrue() g.Assert(err2 == nil).IsTrue() g.Assert(key.ID).Equal(getkey.ID) g.Assert(key.Public).Equal(getkey.Public) g.Assert(key.Private).Equal(getkey.Private) }) g.It("Should get a key", func() { key := model.Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err := s.Keys().Create(&key) g.Assert(err == nil).IsTrue() g.Assert(key.ID != 0).IsTrue() getkey, err := s.Keys().Get(&model.Repo{ID: 1}) g.Assert(err == nil).IsTrue() g.Assert(key.ID).Equal(getkey.ID) g.Assert(key.Public).Equal(getkey.Public) g.Assert(key.Private).Equal(getkey.Private) }) g.It("Should delete a key", func() { key := model.Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err1 := s.Keys().Create(&key) err2 := s.Keys().Delete(&key) g.Assert(err1 == nil).IsTrue() g.Assert(err2 == nil).IsTrue() _, err := s.Keys().Get(&model.Repo{ID: 1}) g.Assert(err == nil).IsFalse() }) }) }