Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 1 | package sarama |
| 2 | |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 3 | //Resource holds information about acl resource type |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 4 | type Resource struct { |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 5 | ResourceType AclResourceType |
| 6 | ResourceName string |
| 7 | ResourcePatternType AclResourcePatternType |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 8 | } |
| 9 | |
| 10 | func (r *Resource) encode(pe packetEncoder, version int16) error { |
| 11 | pe.putInt8(int8(r.ResourceType)) |
| 12 | |
| 13 | if err := pe.putString(r.ResourceName); err != nil { |
| 14 | return err |
| 15 | } |
| 16 | |
| 17 | if version == 1 { |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 18 | if r.ResourcePatternType == AclPatternUnknown { |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 19 | Logger.Print("Cannot encode an unknown resource pattern type, using Literal instead") |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 20 | r.ResourcePatternType = AclPatternLiteral |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 21 | } |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 22 | pe.putInt8(int8(r.ResourcePatternType)) |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | func (r *Resource) decode(pd packetDecoder, version int16) (err error) { |
| 29 | resourceType, err := pd.getInt8() |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | r.ResourceType = AclResourceType(resourceType) |
| 34 | |
| 35 | if r.ResourceName, err = pd.getString(); err != nil { |
| 36 | return err |
| 37 | } |
| 38 | if version == 1 { |
| 39 | pattern, err := pd.getInt8() |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 43 | r.ResourcePatternType = AclResourcePatternType(pattern) |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | return nil |
| 47 | } |
| 48 | |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 49 | //Acl holds information about acl type |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 50 | type Acl struct { |
| 51 | Principal string |
| 52 | Host string |
| 53 | Operation AclOperation |
| 54 | PermissionType AclPermissionType |
| 55 | } |
| 56 | |
| 57 | func (a *Acl) encode(pe packetEncoder) error { |
| 58 | if err := pe.putString(a.Principal); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | if err := pe.putString(a.Host); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | pe.putInt8(int8(a.Operation)) |
| 67 | pe.putInt8(int8(a.PermissionType)) |
| 68 | |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | func (a *Acl) decode(pd packetDecoder, version int16) (err error) { |
| 73 | if a.Principal, err = pd.getString(); err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | if a.Host, err = pd.getString(); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | operation, err := pd.getInt8() |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | a.Operation = AclOperation(operation) |
| 86 | |
| 87 | permissionType, err := pd.getInt8() |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | a.PermissionType = AclPermissionType(permissionType) |
| 92 | |
| 93 | return nil |
| 94 | } |
| 95 | |
Scott Baker | 8487c5d | 2019-10-18 12:49:46 -0700 | [diff] [blame^] | 96 | //ResourceAcls is an acl resource type |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 97 | type ResourceAcls struct { |
| 98 | Resource |
| 99 | Acls []*Acl |
| 100 | } |
| 101 | |
| 102 | func (r *ResourceAcls) encode(pe packetEncoder, version int16) error { |
| 103 | if err := r.Resource.encode(pe, version); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | if err := pe.putArrayLength(len(r.Acls)); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | for _, acl := range r.Acls { |
| 111 | if err := acl.encode(pe); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | func (r *ResourceAcls) decode(pd packetDecoder, version int16) error { |
| 120 | if err := r.Resource.decode(pd, version); err != nil { |
| 121 | return err |
| 122 | } |
| 123 | |
| 124 | n, err := pd.getArrayLength() |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | |
| 129 | r.Acls = make([]*Acl, n) |
| 130 | for i := 0; i < n; i++ { |
| 131 | r.Acls[i] = new(Acl) |
| 132 | if err := r.Acls[i].decode(pd, version); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return nil |
| 138 | } |