package weed_server import ( "context" "net/http/httptest" "strings" "testing" "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" "github.com/seaweedfs/seaweedfs/weed/security" ) // A token looked up for a file id, over gRPC or /dir/lookup, must pass the // volume server's check for that file, a file id with a "_" suffix // included. func TestLookupVolumeTokenAuthorizesTheFileId(t *testing.T) { ms := newLeaderMaster() ms.guard = security.NewGuard(nil, "write-key", 10, "", 0, "", "") node := ms.Topo.GetOrCreateDataCenter("dc1").GetOrCreateRack("rack1"). GetOrCreateDataNode("127.0.0.1", 8080, 18080, "127.0.0.1", "node1", map[string]uint32{"": 10}) ms.Topo.SyncDataNodeRegistration([]*master_pb.VolumeInformationMessage{{Id: 7, Size: 100}}, node) for _, fileId := range []string{"7,01637037d6", "7,01637037d6_1"} { resp, err := ms.LookupVolume(context.Background(), &master_pb.LookupVolumeRequest{VolumeOrFileIds: []string{fileId}}) if err != nil { t.Fatalf("lookup %s: %v", fileId, err) } if len(resp.VolumeIdLocations) != 1 || resp.VolumeIdLocations[0].Auth == "" { t.Fatalf("lookup %s answer = %+v, want one location with a token", fileId, resp.VolumeIdLocations) } if !volumeAccepts(resp.VolumeIdLocations[0].Auth, fileId) { t.Errorf("the volume server refuses the gRPC token looked up for %s", fileId) } w := httptest.NewRecorder() ms.maybeAddJwtAuthorization(w, fileId, true) if token := strings.TrimPrefix(w.Header().Get("Authorization"), security.BearerPrefix); !volumeAccepts(token, fileId) { t.Errorf("the volume server refuses the /dir/lookup token for %s", fileId) } } } func volumeAccepts(token, fileId string) bool { vid, fid, _ := strings.Cut(fileId, ",") r := httptest.NewRequest("DELETE", "/"+fileId, nil) r.Header.Set("Authorization", security.BearerPrefix+token) return (&VolumeServer{}).checkJwtAuthorizationWithKey(r, vid, fid, security.SigningKey("write-key"), nil) }