github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/auth/arn_test.go (about)

     1  package auth_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/treeverse/lakefs/pkg/auth"
     7  )
     8  
     9  func TestParseARN(t *testing.T) {
    10  	cases := []struct {
    11  		Input string
    12  		Arn   auth.Arn
    13  		Error bool
    14  	}{
    15  		{Input: "", Error: true},
    16  		{Input: "arn:lakefs:repo", Error: true},
    17  		{Input: "arn:lakefs:repos:a:b:myrepo", Arn: auth.Arn{
    18  			Partition:  "lakefs",
    19  			Service:    "repos",
    20  			Region:     "a",
    21  			AccountID:  "b",
    22  			ResourceID: "myrepo"}},
    23  		{Input: "arn:lakefs:repos:a::myrepo", Arn: auth.Arn{
    24  			Partition:  "lakefs",
    25  			Service:    "repos",
    26  			Region:     "a",
    27  			AccountID:  "",
    28  			ResourceID: "myrepo"}},
    29  		{Input: "arn:lakefs:repos::b:myrepo", Arn: auth.Arn{
    30  			Partition:  "lakefs",
    31  			Service:    "repos",
    32  			Region:     "",
    33  			AccountID:  "b",
    34  			ResourceID: "myrepo"}},
    35  		{Input: "arn:lakefs:repos:::myrepo", Arn: auth.Arn{
    36  			Partition:  "lakefs",
    37  			Service:    "repos",
    38  			Region:     "",
    39  			AccountID:  "",
    40  			ResourceID: "myrepo"}},
    41  		{Input: "arn:lakefs:fs:::myrepo/branch/file:with:colon", Arn: auth.Arn{
    42  			Partition:  "lakefs",
    43  			Service:    "fs",
    44  			Region:     "",
    45  			AccountID:  "",
    46  			ResourceID: "myrepo/branch/file:with:colon"}},
    47  	}
    48  
    49  	for _, c := range cases {
    50  		got, err := auth.ParseARN(c.Input)
    51  		if err != nil && !c.Error {
    52  			t.Fatalf("got unexpected error parsing arn: \"%s\": \"%s\"", c.Input, err)
    53  		} else if err != nil {
    54  			continue
    55  		} else if c.Error {
    56  			t.Fatalf("expected error parsing arn: \"%s\"", c.Input)
    57  		}
    58  		if got.AccountID != c.Arn.AccountID {
    59  			t.Fatalf("got unexpected account ID parsing arn: \"%s\": \"%s\" (expected \"%s\")", c.Input, got.AccountID, c.Arn.AccountID)
    60  		}
    61  		if got.Region != c.Arn.Region {
    62  			t.Fatalf("got unexpected region parsing arn: \"%s\": \"%s\" (expected \"%s\")", c.Input, got.Region, c.Arn.Region)
    63  		}
    64  		if got.Partition != c.Arn.Partition {
    65  			t.Fatalf("got unexpected partition parsing arn: \"%s\": \"%s\" (expected \"%s\")", c.Input, got.Partition, c.Arn.Partition)
    66  		}
    67  		if got.Service != c.Arn.Service {
    68  			t.Fatalf("got unexpected service parsing arn: \"%s\": \"%s\" (expected \"%s\")", c.Input, got.Service, c.Arn.Service)
    69  		}
    70  		if got.ResourceID != c.Arn.ResourceID {
    71  			t.Fatalf("got unexpected resource ID parsing arn: \"%s\": \"%s\" (expected \"%s\")", c.Input, got.ResourceID, c.Arn.ResourceID)
    72  		}
    73  	}
    74  }
    75  
    76  func TestArnMatch(t *testing.T) {
    77  	cases := []struct {
    78  		InputSource      string
    79  		InputDestination string
    80  		Match            bool
    81  	}{
    82  		{"arn:lakefs:repos::b:myrepo", "arn:lakefs:repos::b:myrepo", true},
    83  		{"arn:lakefs:repos::b:*", "arn:lakefs:repos::b:myrepo", true},
    84  		{"arn:lakefs:repos::b:my*", "arn:lakefs:repos::b:myrepo", true},
    85  		{"arn:lakefs:repos::b:my*po", "arn:lakefs:repos::b:myrepo", true},
    86  		{"arn:lakefs:repos::b:our*", "arn:lakefs:repos::b:myrepo", false},
    87  		{"arn:lakefs:repos::b:my*own", "arn:lakefs:repos::b:myrepo", false},
    88  		{"arn:lakefs:repos::b:myrepo", "arn:lakefs:repos::b:*", false},
    89  		{"arn:lakefs:repo:::*", "arn:lakefs:repo:::*", true},
    90  		{"arn:lakefs:repo", "arn:lakefs:repo", false},
    91  	}
    92  
    93  	for _, c := range cases {
    94  		got := auth.ArnMatch(c.InputSource, c.InputDestination)
    95  		if got != c.Match {
    96  			t.Fatalf("expected match %v, got %v on source = %s, destination = %s", c.Match, got, c.InputSource, c.InputDestination)
    97  		}
    98  	}
    99  }