remove rawFileName from Zone db schema as no longer necessary, removed not needed code, and documentation. Made tests work with new zone file route consolidation.

This commit is contained in:
grumbulon 2023-02-04 23:02:21 -05:00 committed by Gitea
parent bca521eb4e
commit cf1071b389
5 changed files with 36 additions and 76 deletions

View File

@ -313,40 +313,6 @@ func (a *accountTest) TestUpload(t *testing.T) {
assert.Equal(t, tc.expected.response, resp.StatusCode)
}
if tc.name == "Should upload a valid file" {
parse(t, f.Name(), a)
}
})
}
}
func parse(t *testing.T, fname string, a *accountTest) {
var target response
client := http.Client{}
form := url.Values{}
form.Add("filename", filepath.Clean(fname))
if req, err := http.NewRequest(http.MethodPost, a.url+`/api/parse`, strings.NewReader(form.Encode())); err == nil {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", `Bearer:`+a.token)
req.Header.Add("User-Agent", "pomme-api-test-slave")
resp, err := client.Do(req)
if err != nil {
assert.NotNil(t, err)
}
respBody, _ := io.ReadAll(resp.Body)
err = json.Unmarshal(respBody, &target)
if err != nil {
assert.NotNil(t, err)
}
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}
}

View File

@ -12,15 +12,8 @@ import (
var errEmptyFile = errors.New("will not save empty file to FS")
// makeLocal takes a type path and then saves a zone file to either tmp or a permanent location.
func makeLocal(where path, zone *ZoneRequest) error {
var (
path string
file string
c *internal.Config
err error
)
if _, err = os.Stat(fmt.Sprintf(zone.FileName, zone.User)); !os.IsNotExist(err) {
func makeLocal(zone *ZoneRequest) error {
if _, err := os.Stat(fmt.Sprintf(zone.FileName, zone.User)); !os.IsNotExist(err) {
return fmt.Errorf("file %s already exists: %w", zone.FileName, err)
}
@ -28,25 +21,21 @@ func makeLocal(where path, zone *ZoneRequest) error {
return errEmptyFile
}
c, err = internal.ReadConfig()
c, err := internal.ReadConfig()
if err != nil {
logHandler(genericResponseFields{"error": err.Error(), "message": "no config file defined"})
return fmt.Errorf("unable to parse directory: %w", err)
}
path = fmt.Sprintf("%s/%s/", c.ZoneDir, zone.RawFileName)
file = zone.RawFileName
path := fmt.Sprintf("%s/%s/", c.ZoneDir, zone.FileName)
if err = os.MkdirAll(path, 0o750); err != nil {
logHandler(genericResponseFields{"error": err.Error(), "message": "unable to make directory for zone files"})
return fmt.Errorf("unable to make zone directory: %w", err)
}
f, err := os.Create(filepath.Clean(path + file)) //nolint: gosec
f, err := os.Create(filepath.Clean(path + zone.FileName)) //nolint: gosec
if err != nil {
return fmt.Errorf("failed to write file locally: %w", err)
}

View File

@ -6,39 +6,48 @@ const (
keyPrincipalContextID key = iota
)
const (
Tmp path = iota
Perm
)
type key int
type path int
// ZoneRequest represents a Zone file request.
// ZoneRequest represents a new zone file request.
//
// Inside it is a pointer to the zone struct, which contains zone file information, and a User field to keep track of whom owns the file/request.
type ZoneRequest struct {
*Zone
User string `json:"user,omitempty" gorm:"foreignKey:username;references:User"`
}
// Zone struct represents a zonefile.
// Zone struct represents a zone file.
type Zone struct {
gorm.Model
FileName string `json:"name"`
RawFileName string `json:"rawname"`
Body string `json:"body,omitempty"`
// FileName is the file name for an uploaded zone file how it is expected to show up on the filesystem
FileName string `json:"name"`
// Body is the bytes array of a zone files body for copying and moving it around
Body []byte `json:"body,omitempty"`
}
// GenericResponse is a generics wrapper to send responses for API Errors.
type GenericResponse[T map[string]any] struct {
Response map[string]any `json:"response,omitempty"`
}
// instead of calling map[string]any{...} you can call genericResponseFields{...} when making a generic response above.
type genericResponseFields map[string]any
// ndr is an interface for new DNS requests. It's methods can be used with a ZoneRequest object.
type ndr interface {
// parse() is a wrapper around miekg's NewZoneParser, which is used to validate uploaded zone files
//
// if no error is raised the zone file can be saved.
parse() error
save(where path) error
// save() is a wrapper around internal.makeLocal() which will save a valid non-empty zone file to the filesystem
//
// the file is saved to a location the user sets up in their config.yaml file,
// once saved it can be consumed by something like NSD.
save() error
}
var _ ndr = (*ZoneRequest)(nil)

View File

@ -83,12 +83,11 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
&ZoneRequest{
User: claims["username"].(string),
Zone: &Zone{
FileName: fmt.Sprintf("tmpfile-%s-%s", header.Filename, claims["username"].(string)),
RawFileName: header.Filename,
FileName: header.Filename,
},
})
if err := zoneFile.save(Perm); err != nil {
if err := zoneFile.save(); err != nil {
APIError(w, r, genericResponseFields{"message": "Unable to save zonefile", "status": http.StatusInternalServerError, "error": err.Error()})
return
@ -109,16 +108,14 @@ func newDNSRequest(filename string, user string, dat []byte) ndr {
return &ZoneRequest{
User: user,
Zone: &Zone{
FileName: fmt.Sprintf("tmpfile-%s-%s", filename, user),
RawFileName: filename,
Body: string(dat),
FileName: filename,
Body: dat,
},
}
}
// Parse will be used to parse zonefiles.
func (zone *ZoneRequest) parse() error {
zp := dns.NewZoneParser(strings.NewReader(zone.Body), "", "")
zp := dns.NewZoneParser(strings.NewReader(string(zone.Body)), "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
log.Println(rr)
@ -131,8 +128,8 @@ func (zone *ZoneRequest) parse() error {
return nil
}
func (zone *ZoneRequest) save(p path) error {
return makeLocal(p, zone)
func (zone *ZoneRequest) save() error {
return makeLocal(zone)
}
func validateContentType(file io.Reader) bool {

View File

@ -24,9 +24,8 @@ type ZoneRequest struct {
// Zone struct represents a zonefile in the database.
type Zone struct {
gorm.Model
FileName string `json:"name"`
RawFileName string `json:"rawfilename"`
Body string `json:"body"`
FileName string `json:"name"`
Body string `json:"body"`
}
type Config struct {