@@ -40,9 +40,15 @@ type Secret struct {
4040 RepoID int64 `xorm:"INDEX UNIQUE(owner_repo_name) NOT NULL DEFAULT 0"`
4141 Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
4242 Data string `xorm:"LONGTEXT"` // encrypted data
43+ Description string `xorm:"TEXT"`
4344 CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
4445}
4546
47+ const (
48+ SecretDataMaxLength = 65536
49+ SecretDescriptionMaxLength = 4096
50+ )
51+
4652// ErrSecretNotFound represents a "secret not found" error.
4753type ErrSecretNotFound struct {
4854 Name string
@@ -57,7 +63,7 @@ func (err ErrSecretNotFound) Unwrap() error {
5763}
5864
5965// InsertEncryptedSecret Creates, encrypts, and validates a new secret with yet unencrypted data and insert into database
60- func InsertEncryptedSecret (ctx context.Context , ownerID , repoID int64 , name , data string ) (* Secret , error ) {
66+ func InsertEncryptedSecret (ctx context.Context , ownerID , repoID int64 , name , data , description string ) (* Secret , error ) {
6167 if ownerID != 0 && repoID != 0 {
6268 // It's trying to create a secret that belongs to a repository, but OwnerID has been set accidentally.
6369 // Remove OwnerID to avoid confusion; it's not worth returning an error here.
@@ -67,15 +73,23 @@ func InsertEncryptedSecret(ctx context.Context, ownerID, repoID int64, name, dat
6773 return nil , fmt .Errorf ("%w: ownerID and repoID cannot be both zero, global secrets are not supported" , util .ErrInvalidArgument )
6874 }
6975
76+ if len (data ) > SecretDataMaxLength {
77+ return nil , util .NewInvalidArgumentErrorf ("data too long" )
78+ }
79+
80+ description = util .TruncateRunes (description , SecretDescriptionMaxLength )
81+
7082 encrypted , err := secret_module .EncryptSecret (setting .SecretKey , data )
7183 if err != nil {
7284 return nil , err
7385 }
86+
7487 secret := & Secret {
75- OwnerID : ownerID ,
76- RepoID : repoID ,
77- Name : strings .ToUpper (name ),
78- Data : encrypted ,
88+ OwnerID : ownerID ,
89+ RepoID : repoID ,
90+ Name : strings .ToUpper (name ),
91+ Data : encrypted ,
92+ Description : description ,
7993 }
8094 return secret , db .Insert (ctx , secret )
8195}
@@ -114,16 +128,23 @@ func (opts FindSecretsOptions) ToConds() builder.Cond {
114128}
115129
116130// UpdateSecret changes org or user reop secret.
117- func UpdateSecret (ctx context.Context , secretID int64 , data string ) error {
131+ func UpdateSecret (ctx context.Context , secretID int64 , data , description string ) error {
132+ if len (data ) > SecretDataMaxLength {
133+ return util .NewInvalidArgumentErrorf ("data too long" )
134+ }
135+
136+ description = util .TruncateRunes (description , SecretDescriptionMaxLength )
137+
118138 encrypted , err := secret_module .EncryptSecret (setting .SecretKey , data )
119139 if err != nil {
120140 return err
121141 }
122142
123143 s := & Secret {
124- Data : encrypted ,
144+ Data : encrypted ,
145+ Description : description ,
125146 }
126- affected , err := db .GetEngine (ctx ).ID (secretID ).Cols ("data" ).Update (s )
147+ affected , err := db .GetEngine (ctx ).ID (secretID ).Cols ("data" , "description" ).Update (s )
127148 if affected != 1 {
128149 return ErrSecretNotFound {}
129150 }
0 commit comments