github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/acl/marshal.go (about) 1 package acl 2 3 import ( 4 acl "github.com/TrueCloudLab/frostfs-api-go/v2/acl/grpc" 5 "github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message" 6 protoutil "github.com/TrueCloudLab/frostfs-api-go/v2/util/proto" 7 ) 8 9 const ( 10 filterHeaderTypeField = 1 11 filterMatchTypeField = 2 12 filterNameField = 3 13 filterValueField = 4 14 15 targetTypeField = 1 16 targetKeysField = 2 17 18 recordOperationField = 1 19 recordActionField = 2 20 recordFiltersField = 3 21 recordTargetsField = 4 22 23 tableVersionField = 1 24 tableContainerIDField = 2 25 tableRecordsField = 3 26 27 lifetimeExpirationField = 1 28 lifetimeNotValidBeforeField = 2 29 lifetimeIssuedAtField = 3 30 31 bearerTokenBodyACLField = 1 32 bearerTokenBodyOwnerField = 2 33 bearerTokenBodyLifetimeField = 3 34 35 bearerTokenBodyField = 1 36 bearerTokenSignatureField = 2 37 ) 38 39 // StableMarshal marshals unified acl table structure in a protobuf 40 // compatible way without field order shuffle. 41 func (t *Table) StableMarshal(buf []byte) []byte { 42 if t == nil { 43 return []byte{} 44 } 45 46 if buf == nil { 47 buf = make([]byte, t.StableSize()) 48 } 49 50 var offset int 51 52 offset += protoutil.NestedStructureMarshal(tableVersionField, buf[offset:], t.version) 53 offset += protoutil.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid) 54 55 for i := range t.records { 56 offset += protoutil.NestedStructureMarshal(tableRecordsField, buf[offset:], &t.records[i]) 57 } 58 59 return buf 60 } 61 62 // StableSize of acl table structure marshalled by StableMarshal function. 63 func (t *Table) StableSize() (size int) { 64 if t == nil { 65 return 0 66 } 67 68 size += protoutil.NestedStructureSize(tableVersionField, t.version) 69 size += protoutil.NestedStructureSize(tableContainerIDField, t.cid) 70 71 for i := range t.records { 72 size += protoutil.NestedStructureSize(tableRecordsField, &t.records[i]) 73 } 74 75 return size 76 } 77 78 func (t *Table) Unmarshal(data []byte) error { 79 return message.Unmarshal(t, data, new(acl.EACLTable)) 80 } 81 82 // StableMarshal marshals unified acl record structure in a protobuf 83 // compatible way without field order shuffle. 84 func (r *Record) StableMarshal(buf []byte) []byte { 85 if r == nil { 86 return []byte{} 87 } 88 89 if buf == nil { 90 buf = make([]byte, r.StableSize()) 91 } 92 93 var offset int 94 95 offset += protoutil.EnumMarshal(recordOperationField, buf[offset:], int32(r.op)) 96 offset += protoutil.EnumMarshal(recordActionField, buf[offset:], int32(r.action)) 97 98 for i := range r.filters { 99 offset += protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], &r.filters[i]) 100 } 101 102 for i := range r.targets { 103 offset += protoutil.NestedStructureMarshal(recordTargetsField, buf[offset:], &r.targets[i]) 104 } 105 106 return buf 107 } 108 109 // StableSize of acl record structure marshalled by StableMarshal function. 110 func (r *Record) StableSize() (size int) { 111 if r == nil { 112 return 0 113 } 114 115 size += protoutil.EnumSize(recordOperationField, int32(r.op)) 116 size += protoutil.EnumSize(recordActionField, int32(r.action)) 117 118 for i := range r.filters { 119 size += protoutil.NestedStructureSize(recordFiltersField, &r.filters[i]) 120 } 121 122 for i := range r.targets { 123 size += protoutil.NestedStructureSize(recordTargetsField, &r.targets[i]) 124 } 125 126 return size 127 } 128 129 func (r *Record) Unmarshal(data []byte) error { 130 return message.Unmarshal(r, data, new(acl.EACLRecord)) 131 } 132 133 // StableMarshal marshals unified header filter structure in a protobuf 134 // compatible way without field order shuffle. 135 func (f *HeaderFilter) StableMarshal(buf []byte) []byte { 136 if f == nil { 137 return []byte{} 138 } 139 140 if buf == nil { 141 buf = make([]byte, f.StableSize()) 142 } 143 144 var offset int 145 146 offset += protoutil.EnumMarshal(filterHeaderTypeField, buf[offset:], int32(f.hdrType)) 147 offset += protoutil.EnumMarshal(filterMatchTypeField, buf[offset:], int32(f.matchType)) 148 offset += protoutil.StringMarshal(filterNameField, buf[offset:], f.key) 149 protoutil.StringMarshal(filterValueField, buf[offset:], f.value) 150 151 return buf 152 } 153 154 // StableSize of header filter structure marshalled by StableMarshal function. 155 func (f *HeaderFilter) StableSize() (size int) { 156 if f == nil { 157 return 0 158 } 159 160 size += protoutil.EnumSize(filterHeaderTypeField, int32(f.hdrType)) 161 size += protoutil.EnumSize(filterMatchTypeField, int32(f.matchType)) 162 size += protoutil.StringSize(filterNameField, f.key) 163 size += protoutil.StringSize(filterValueField, f.value) 164 165 return size 166 } 167 168 func (f *HeaderFilter) Unmarshal(data []byte) error { 169 return message.Unmarshal(f, data, new(acl.EACLRecord_Filter)) 170 } 171 172 // StableMarshal marshals unified role info structure in a protobuf 173 // compatible way without field order shuffle. 174 func (t *Target) StableMarshal(buf []byte) []byte { 175 if t == nil { 176 return []byte{} 177 } 178 179 if buf == nil { 180 buf = make([]byte, t.StableSize()) 181 } 182 183 var offset int 184 185 offset += protoutil.EnumMarshal(targetTypeField, buf[offset:], int32(t.role)) 186 protoutil.RepeatedBytesMarshal(targetKeysField, buf[offset:], t.keys) 187 188 return buf 189 } 190 191 // StableSize of role info structure marshalled by StableMarshal function. 192 func (t *Target) StableSize() (size int) { 193 if t == nil { 194 return 0 195 } 196 197 size += protoutil.EnumSize(targetTypeField, int32(t.role)) 198 size += protoutil.RepeatedBytesSize(targetKeysField, t.keys) 199 200 return size 201 } 202 203 func (t *Target) Unmarshal(data []byte) error { 204 return message.Unmarshal(t, data, new(acl.EACLRecord_Target)) 205 } 206 207 func (l *TokenLifetime) StableMarshal(buf []byte) []byte { 208 if l == nil { 209 return []byte{} 210 } 211 212 if buf == nil { 213 buf = make([]byte, l.StableSize()) 214 } 215 216 var offset int 217 218 offset += protoutil.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp) 219 offset += protoutil.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) 220 protoutil.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) 221 222 return buf 223 } 224 225 func (l *TokenLifetime) StableSize() (size int) { 226 if l == nil { 227 return 0 228 } 229 230 size += protoutil.UInt64Size(lifetimeExpirationField, l.exp) 231 size += protoutil.UInt64Size(lifetimeNotValidBeforeField, l.nbf) 232 size += protoutil.UInt64Size(lifetimeIssuedAtField, l.iat) 233 234 return size 235 } 236 237 func (l *TokenLifetime) Unmarshal(data []byte) error { 238 return message.Unmarshal(l, data, new(acl.BearerToken_Body_TokenLifetime)) 239 } 240 241 func (bt *BearerTokenBody) StableMarshal(buf []byte) []byte { 242 if bt == nil { 243 return []byte{} 244 } 245 246 if buf == nil { 247 buf = make([]byte, bt.StableSize()) 248 } 249 250 var offset int 251 252 offset += protoutil.NestedStructureMarshal(bearerTokenBodyACLField, buf[offset:], bt.eacl) 253 offset += protoutil.NestedStructureMarshal(bearerTokenBodyOwnerField, buf[offset:], bt.ownerID) 254 protoutil.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf[offset:], bt.lifetime) 255 256 return buf 257 } 258 259 func (bt *BearerTokenBody) StableSize() (size int) { 260 if bt == nil { 261 return 0 262 } 263 264 size += protoutil.NestedStructureSize(bearerTokenBodyACLField, bt.eacl) 265 size += protoutil.NestedStructureSize(bearerTokenBodyOwnerField, bt.ownerID) 266 size += protoutil.NestedStructureSize(bearerTokenBodyLifetimeField, bt.lifetime) 267 268 return size 269 } 270 271 func (bt *BearerTokenBody) Unmarshal(data []byte) error { 272 return message.Unmarshal(bt, data, new(acl.BearerToken_Body)) 273 } 274 275 func (bt *BearerToken) StableMarshal(buf []byte) []byte { 276 if bt == nil { 277 return []byte{} 278 } 279 280 if buf == nil { 281 buf = make([]byte, bt.StableSize()) 282 } 283 284 var offset int 285 286 offset += protoutil.NestedStructureMarshal(bearerTokenBodyField, buf[offset:], bt.body) 287 protoutil.NestedStructureMarshal(bearerTokenSignatureField, buf[offset:], bt.sig) 288 289 return buf 290 } 291 292 func (bt *BearerToken) StableSize() (size int) { 293 if bt == nil { 294 return 0 295 } 296 297 size += protoutil.NestedStructureSize(bearerTokenBodyField, bt.body) 298 size += protoutil.NestedStructureSize(bearerTokenSignatureField, bt.sig) 299 300 return size 301 } 302 303 func (bt *BearerToken) Unmarshal(data []byte) error { 304 return message.Unmarshal(bt, data, new(acl.BearerToken)) 305 }