github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/remotesrv/validate.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package remotesrv 16 17 import ( 18 "fmt" 19 20 remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1" 21 "github.com/dolthub/dolt/go/store/hash" 22 "github.com/dolthub/dolt/go/store/types" 23 ) 24 25 func validateRepoRequest(req repoRequest) error { 26 if req.GetRepoPath() == "" && req.GetRepoId() == nil { 27 return fmt.Errorf("expected repo_path or repo_id, got neither") 28 } else if req.GetRepoPath() == "" { 29 id := req.GetRepoId() 30 if id.Org == "" || id.RepoName == "" { 31 return fmt.Errorf("expected repo_id.org and repo_id.repo_name, missing at least one") 32 } 33 } 34 return nil 35 } 36 37 func validateChunkTableInfo(field string, ctis []*remotesapi.ChunkTableInfo) error { 38 for i, cti := range ctis { 39 if len(cti.Hash) != hash.ByteLen { 40 return fmt.Errorf("expected %s[%d].Hash to be %d bytes long, was %d", field, i, hash.ByteLen, len(cti.Hash)) 41 } 42 if cti.ChunkCount == 0 { 43 return fmt.Errorf("expected %s[%d].ChunkCount to be non-zero", field, i) 44 } 45 } 46 return nil 47 } 48 49 func validateHash(field string, h []byte) error { 50 if len(h) != hash.ByteLen { 51 return fmt.Errorf("expected %s hash to be %d bytes long, was %d", field, hash.ByteLen, len(h)) 52 } 53 return nil 54 } 55 56 func validateHashes(field string, hashes [][]byte) error { 57 for i, bs := range hashes { 58 if len(bs) != hash.ByteLen { 59 return fmt.Errorf("expected %s[%d] hash to be %d bytes long, was %d", field, i, hash.ByteLen, len(bs)) 60 } 61 } 62 return nil 63 } 64 65 func ValidateGetRepoMetadataRequest(req *remotesapi.GetRepoMetadataRequest) error { 66 if err := validateRepoRequest(req); err != nil { 67 return err 68 } 69 if req.ClientRepoFormat == nil { 70 return fmt.Errorf("expected non-nil client_repo_format") 71 } 72 if _, err := types.GetFormatForVersionString(req.ClientRepoFormat.NbfVersion); err != nil { 73 return fmt.Errorf("unsupported value for client_repo_format.nbf_version: %w", err) 74 } 75 if req.ClientRepoFormat.NbsVersion != "4" && req.ClientRepoFormat.NbsVersion != "5" { 76 return fmt.Errorf("unsupported value for client_repo_format.nbs_version: %v; expected \"4\" or \"5\"", req.ClientRepoFormat.NbsVersion) 77 } 78 return nil 79 } 80 81 func ValidateHasChunksRequest(req *remotesapi.HasChunksRequest) error { 82 if err := validateRepoRequest(req); err != nil { 83 return err 84 } 85 return validateHashes("hashes", req.Hashes) 86 } 87 88 func ValidateGetDownloadLocsRequest(req *remotesapi.GetDownloadLocsRequest) error { 89 if err := validateRepoRequest(req); err != nil { 90 return err 91 } 92 return validateHashes("chunk_hashes", req.ChunkHashes) 93 } 94 95 func ValidateGetUploadLocsRequest(req *remotesapi.GetUploadLocsRequest) error { 96 if err := validateRepoRequest(req); err != nil { 97 return err 98 } 99 return validateHashes("table_file_hashes", req.TableFileHashes) 100 } 101 102 func ValidateRebaseRequest(req *remotesapi.RebaseRequest) error { 103 if err := validateRepoRequest(req); err != nil { 104 return err 105 } 106 return nil 107 } 108 109 func ValidateRootRequest(req *remotesapi.RootRequest) error { 110 if err := validateRepoRequest(req); err != nil { 111 return err 112 } 113 return nil 114 } 115 116 func ValidateCommitRequest(req *remotesapi.CommitRequest) error { 117 if err := validateRepoRequest(req); err != nil { 118 return err 119 } 120 if err := validateHash("current", req.Current); err != nil { 121 return err 122 } 123 if err := validateHash("last", req.Last); err != nil { 124 return err 125 } 126 if err := validateChunkTableInfo("chunk_table_info", req.ChunkTableInfo); err != nil { 127 return err 128 } 129 return nil 130 } 131 132 func ValidateListTableFilesRequest(req *remotesapi.ListTableFilesRequest) error { 133 if err := validateRepoRequest(req); err != nil { 134 return err 135 } 136 return nil 137 } 138 139 func ValidateRefreshTableFileUrlRequest(req *remotesapi.RefreshTableFileUrlRequest) error { 140 if err := validateRepoRequest(req); err != nil { 141 return err 142 } 143 if req.FileId == "" { 144 return fmt.Errorf("expected file_id") 145 } 146 return nil 147 } 148 149 func ValidateAddTableFilesRequest(req *remotesapi.AddTableFilesRequest) error { 150 if err := validateRepoRequest(req); err != nil { 151 return err 152 } 153 if err := validateChunkTableInfo("chunk_table_info", req.ChunkTableInfo); err != nil { 154 return err 155 } 156 return nil 157 }