acl_bindings.go 2.7 KB

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