modifyACLEntries replaces groot's and guardians' UIDs in the exported files to the UIDs in this dgraph cluster.
(c *LocalCluster, r io.Reader)
| 329 | // modifyACLEntries replaces groot's and guardians' UIDs |
| 330 | // in the exported files to the UIDs in this dgraph cluster. |
| 331 | func modifyACLEntries(c *LocalCluster, r io.Reader) (io.Reader, error) { |
| 332 | grootUIDNew, guardiansUIDNew, err := findGrootAndGuardians(c) |
| 333 | if err != nil { |
| 334 | return nil, err |
| 335 | } |
| 336 | grootUIDNew = fmt.Sprintf("<%v>", grootUIDNew) |
| 337 | guardiansUIDNew = fmt.Sprintf("<%v>", guardiansUIDNew) |
| 338 | |
| 339 | data, err := readGzData(r, c.conf.encryption, c.encKeyPath) |
| 340 | if err != nil { |
| 341 | return nil, err |
| 342 | } |
| 343 | |
| 344 | // We need to find UIDs of the guardians group and groot node and replace them with |
| 345 | // the right UID that the new cluster has. Because,all of the reserved predicates are |
| 346 | // assigned to group 1 and we only have one rdf.gz file per group in the export, we |
| 347 | // can just do it one io.Reader (flie) at a time as we get in this function. The way |
| 348 | // we find the UIDs is through searching for following byte sequences. |
| 349 | // <dgraph.xid> "guardians" |
| 350 | // <dgraph.xid> "groot" |
| 351 | findUID := func(sub []byte) ([]byte, error) { |
| 352 | i := bytes.Index(data, sub) |
| 353 | if i == -1 { |
| 354 | return nil, errors.Errorf("unable to find data in RDF file: [%v]", string(sub)) |
| 355 | } |
| 356 | var start, end int |
| 357 | for i = i - 1; i >= 0; i-- { |
| 358 | if data[i] == byte('>') { |
| 359 | end = i |
| 360 | } else if data[i] == byte('<') { |
| 361 | start = i |
| 362 | break |
| 363 | } |
| 364 | } |
| 365 | return data[start : end+1], nil |
| 366 | } |
| 367 | guardiansUID, err := findUID([]byte(`<dgraph.xid> "guardians"`)) |
| 368 | if err != nil { |
| 369 | return nil, err |
| 370 | } |
| 371 | grootUID, err := findUID([]byte(`<dgraph.xid> "groot"`)) |
| 372 | if err != nil { |
| 373 | return nil, err |
| 374 | } |
| 375 | |
| 376 | // we should only replace if RDF line is for a reserved type |
| 377 | lines := bytes.Split(data, []byte{'\n'}) |
| 378 | for i, line := range lines { |
| 379 | if bytes.Contains(line, []byte("<dgraph.")) { |
| 380 | line = bytes.ReplaceAll(line, guardiansUID, []byte(guardiansUIDNew)) |
| 381 | line = bytes.ReplaceAll(line, grootUID, []byte(grootUIDNew)) |
| 382 | lines[i] = line |
| 383 | } |
| 384 | } |
| 385 | data = bytes.Join(lines, []byte{'\n'}) |
| 386 | |
| 387 | return writeGzData(data, c.conf.encryption, c.encKeyPath) |
| 388 | } |
no test coverage detected