You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2017/01/25 18:57:30 UTC
[41/50] [abbrv] ambari git commit: AMBARI-19694. Post user creation
hook - input csv generated with READ permissions (Laszlo Puskas via oleewere)
AMBARI-19694. Post user creation hook - input csv generated with READ permissions (Laszlo Puskas via oleewere)
Change-Id: I3ae59596f5817dbd60bed37f259ce54a5f16de39
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/b9200e0e
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/b9200e0e
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/b9200e0e
Branch: refs/heads/branch-dev-patch-upgrade
Commit: b9200e0e57111b33b7b998fa59a5d25c0b700869
Parents: ff4babb
Author: Laszlo Puskas <lp...@hortonworks.com>
Authored: Wed Jan 25 11:01:45 2017 +0100
Committer: oleewere <ol...@gmail.com>
Committed: Wed Jan 25 11:03:58 2017 +0100
----------------------------------------------------------------------
.../users/CsvFilePersisterService.java | 24 +++++-
.../CsvFilePersisterServiceFunctionalTest.java | 91 ++++++++++++++++++++
2 files changed, 113 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/b9200e0e/ambari-server/src/main/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterService.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterService.java
index d8ffe98..fe6bf35 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterService.java
@@ -18,12 +18,21 @@
package org.apache.ambari.server.serveraction.users;
+import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.FileAttribute;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -51,10 +60,21 @@ public class CsvFilePersisterService implements CollectionPersisterService<Strin
this.csvFile = csvFile;
}
+ public Set<PosixFilePermission> getCsvPermissions() {
+ Set<PosixFilePermission> permissionsSet = new HashSet<>();
+ permissionsSet.add(PosixFilePermission.OWNER_READ);
+ permissionsSet.add(PosixFilePermission.OWNER_WRITE);
+ permissionsSet.add(PosixFilePermission.GROUP_READ);
+ permissionsSet.add(PosixFilePermission.OTHERS_READ);
+ return permissionsSet;
+ }
+
@Inject
public void init() throws IOException {
- // make 3rd party dependencies be managed by the container (probably constructor binding or factory is needed)
- fileWriter = new FileWriter(csvFile);
+
+ Path csv = Files.createFile(Paths.get(csvFile), PosixFilePermissions.asFileAttribute(getCsvPermissions()));
+ fileWriter = new FileWriter(csv.toFile());
+
csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR));
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/b9200e0e/ambari-server/src/test/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterServiceFunctionalTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterServiceFunctionalTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterServiceFunctionalTest.java
new file mode 100644
index 0000000..97529fe
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/users/CsvFilePersisterServiceFunctionalTest.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.serveraction.users;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.PosixFilePermission;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.assistedinject.FactoryModuleBuilder;
+
+/**
+ * Test cases for the persiter service implementation.
+ * For fully testing the behavior it builds up GUICE context.
+ */
+public class CsvFilePersisterServiceFunctionalTest {
+
+ private static final String TEST_CSV = "/tmp/users.csv";
+
+ private static Injector injector;
+ private static CollectionPersisterServiceFactory serviceFactory;
+ private CsvFilePersisterService csvFileCsvFilePersisterService;
+ private Path testCsvPath;
+
+ /**
+ * Guice module for testing service / factory behavior
+ */
+ private static class TestPersistServiceModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ install(new FactoryModuleBuilder().implement(CollectionPersisterService.class, CsvFilePersisterService.class).build(CollectionPersisterServiceFactory.class));
+ }
+ }
+
+ @BeforeClass
+ public static void beforeClass() {
+ injector = Guice.createInjector(new TestPersistServiceModule());
+ serviceFactory = (CollectionPersisterServiceFactory) injector.getInstance(CollectionPersisterServiceFactory.class);
+ }
+
+ @Before
+ public void before() {
+ csvFileCsvFilePersisterService = serviceFactory.createCsvFilePersisterService(TEST_CSV);
+ testCsvPath = Paths.get(TEST_CSV);
+ }
+
+ @Test
+ public void shouldCreateCsvFileWithExpectedPermissions() throws IOException {
+
+ Assert.assertNotNull(csvFileCsvFilePersisterService);
+
+ Assert.assertTrue("The generated file couldn't be found", Files.exists(testCsvPath));
+
+ Assert.assertTrue("The generated files doesn't have all the expected permissions", Files.getPosixFilePermissions(testCsvPath).containsAll(csvFileCsvFilePersisterService.getCsvPermissions()));
+
+ Assert.assertFalse("The generated file has more than the required permissions", Files.getPosixFilePermissions(testCsvPath).contains(PosixFilePermission.GROUP_EXECUTE));
+
+ }
+
+ @After
+ public void after() throws IOException {
+ Files.deleteIfExists(Paths.get(TEST_CSV));
+ }
+
+}
\ No newline at end of file