Yair Zaslavsky has uploaded a new change for review.

Change subject: wip: XML file based directory and authenticator
......................................................................

wip: XML file based directory and authenticator

DO NOT REVIEW, STILL CODING!!!

Experimenting authenticator and directory that is
based on XML file.
Basically the file contains users and groups information.
This might be useful for example in POCs of oVirt for quick
setup of users and groups.

the conf file at /etc/ovirt-engine/auth.d should look like:

name=xml
authenticator.type=xml
xml.file=/tmp/directory.xml
directory.type=xml

Change-Id: I379ec6cffc10a8fb456eb3a52666665bf2331e42
Signed-off-by: Yair Zaslavsky <yzasl...@redhat.com>
---
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticator.java
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticatorFactory.java
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectory.java
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryContents.java
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryFactory.java
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlGroup.java
A 
backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlUser.java
7 files changed, 495 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/60/22360/1

diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticator.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticator.java
new file mode 100644
index 0000000..5c04f53
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticator.java
@@ -0,0 +1,60 @@
+package org.ovirt.engine.core.authentication.xml;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.List;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+
+import org.ovirt.engine.core.authentication.PasswordAuthenticator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class XmlAuthenticator implements PasswordAuthenticator {
+
+    private static final Logger log = 
LoggerFactory.getLogger(XmlAuthenticator.class);
+
+    private String authenticatorName;
+    private String xmlFileName;
+
+    public XmlAuthenticator(String authenticatorName, String xmlFileName) {
+        this.authenticatorName = authenticatorName;
+        this.xmlFileName = xmlFileName;
+    }
+
+    @Override
+    public String getName() {
+        return authenticatorName;
+    }
+
+    @Override
+    public boolean authenticate(String name, char[] password) {
+        String passwdStr = new String(password);
+        JAXBContext context;
+        try (FileInputStream is = new FileInputStream(xmlFileName)) {
+            context = JAXBContext.newInstance(XmlDirectoryContents.class);
+            Unmarshaller unmarshaller = context.createUnmarshaller();
+            XmlDirectoryContents contents = (XmlDirectoryContents) 
unmarshaller.unmarshal(is);
+            List<XmlUser> users = contents.getUsers();
+            for (XmlUser user : users) {
+                if (user.getPassword().equals(passwdStr)) {
+                    return true;
+                }
+            }
+        } catch (FileNotFoundException e) {
+            log.error("The XML directory contents file " + xmlFileName + " 
could not be found");
+        } catch (IOException e) {
+            log.error("Some I/O error when reading the directory contents file 
" + xmlFileName + ". The error is "
+                        + e.getMessage());
+        } catch (JAXBException e) {
+            log.error("Some parsing error when parsing the directory contents 
file " + xmlFileName + ". The error is "
+                    + e.getMessage());
+        }
+
+        return false;
+    }
+
+}
diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticatorFactory.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticatorFactory.java
new file mode 100644
index 0000000..8d54942
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlAuthenticatorFactory.java
@@ -0,0 +1,50 @@
+package org.ovirt.engine.core.authentication.xml;
+
+import java.io.File;
+
+import org.ovirt.engine.core.authentication.Authenticator;
+import org.ovirt.engine.core.authentication.AuthenticatorFactory;
+import org.ovirt.engine.core.authentication.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class XmlAuthenticatorFactory implements AuthenticatorFactory {
+
+    private static final Logger log = 
LoggerFactory.getLogger(XmlAuthenticatorFactory.class);
+
+    private static final String NAME_PARAMETER = "name";
+    private static final String XML_FILE_NAME_PARAMETER = "file";
+
+    @Override
+    public String getType() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public Authenticator create(File file, Configuration config) {
+        // Get the name of the authenticator:
+        String name = config.getInheritedString(NAME_PARAMETER);
+        if (name == null) {
+            log.error(
+                    "The configuration file \"{}\" doesn't contain the name of 
the authenticator.",
+                    file.getAbsolutePath()
+                    );
+            return null;
+        }
+
+        // Get the xml file that contains password info:
+        String xmlFileName = 
config.getInheritedString(XML_FILE_NAME_PARAMETER);
+        if (name == null) {
+            log.error(
+                    "The configuration file \"{}\" doesn't contain the xml 
file name.",
+                    file.getAbsolutePath()
+                    );
+            return null;
+        }
+
+        return new XmlAuthenticator(name, xmlFileName);
+    }
+
+}
diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectory.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectory.java
new file mode 100644
index 0000000..9bcc8e9
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectory.java
@@ -0,0 +1,175 @@
+package org.ovirt.engine.core.authentication.xml;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+
+import org.ovirt.engine.core.authentication.Directory;
+import org.ovirt.engine.core.authentication.DirectoryEntryStatus;
+import org.ovirt.engine.core.authentication.DirectoryGroup;
+import org.ovirt.engine.core.authentication.DirectoryUser;
+import org.ovirt.engine.core.common.utils.ExternalId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class XmlDirectory implements Directory {
+
+    private String directoryName;
+    private String xmlFileName;
+    private XmlDirectoryContents contents;
+    private static final Logger log = 
LoggerFactory.getLogger(XmlAuthenticator.class);
+
+    public XmlDirectory(String directoryName, String xmlFileName) {
+        this.directoryName = directoryName;
+        this.xmlFileName = xmlFileName;
+    }
+
+    public void initContents() {
+        JAXBContext context;
+        try (FileInputStream is = new FileInputStream(xmlFileName)) {
+            context = JAXBContext.newInstance(XmlDirectoryContents.class);
+            Unmarshaller unmarshaller = context.createUnmarshaller();
+            contents = (XmlDirectoryContents) unmarshaller.unmarshal(is);
+        } catch (FileNotFoundException e) {
+            log.error("The XML directory contents file " + xmlFileName + " 
could not be found");
+        } catch (IOException e) {
+            log.error("Some I/O error when reading the directory contents file 
" + xmlFileName + ". The error is "
+                        + e.getMessage());
+        } catch (JAXBException e) {
+            log.error("Some parsing error when parsing the directory contents 
file " + xmlFileName + ". The error is "
+                    + e.getMessage());
+        }
+    }
+
+    @Override
+    public String getName() {
+        return directoryName;
+    }
+
+    @Override
+    public DirectoryUser findUser(String name) {
+        for (XmlUser user : contents.getUsers()) {
+            if (user.getUserName().equals(name)) {
+                return mapDirectoryUser(user);
+            }
+        }
+        return null;
+    }
+
+    private DirectoryUser mapDirectoryUser(XmlUser user) {
+        DirectoryUser dirUser =
+                new DirectoryUser(this, mapToExternalId(user.getId()), 
user.getUserName());
+
+        dirUser.setFirstName(user.getName());
+        dirUser.setLastName(user.getLastName());
+        dirUser.setEmail(user.getEmail());
+        dirUser.setDepartment(user.getDepartment());
+        dirUser.setTitle(user.getTitle());
+        dirUser.setStatus(DirectoryEntryStatus.AVAILABLE);
+        dirUser.setAdmin(user.isAdmin());
+        dirUser.setGroups(findGroupsByIds(user.getMemberOf()));
+        return dirUser;
+    }
+
+    private List<DirectoryGroup> findGroupsByIds(String memberOf) {
+        Set<ExternalId> ids = new HashSet<>();
+        List<DirectoryGroup> results = new ArrayList<>();
+        String[] parts = memberOf.split(",");
+        for (String id : parts) {
+            ids.add(mapToExternalId(id));
+        }
+
+        for (XmlGroup group : contents.getGroups()) {
+            if (ids.contains(mapToExternalId(group.getId()))) {
+                results.add(mapDirectoryGroup(group));
+            }
+        }
+        return results;
+    }
+
+    private DirectoryGroup mapDirectoryGroup(XmlGroup group) {
+        DirectoryGroup dirGroup = new DirectoryGroup(this, 
mapToExternalId(group.getId()), group.getName());
+        dirGroup.setStatus(DirectoryEntryStatus.AVAILABLE);
+        return dirGroup;
+    }
+
+    private ExternalId mapToExternalId(String id) {
+        try {
+            return new ExternalId(id.getBytes("UTF-8"));
+        } catch (UnsupportedEncodingException e) {
+            return null;
+        }
+    }
+
+    @Override
+    public DirectoryUser findUser(ExternalId id) {
+        for (XmlUser user : contents.getUsers()) {
+            if (mapToExternalId(user.getId()).equals(id)) {
+                return mapDirectoryUser(user);
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public List<DirectoryUser> findUsers(List<ExternalId> ids) {
+        List<DirectoryUser> users = new ArrayList<DirectoryUser>();
+        Set<ExternalId> idsSet = new HashSet<>(ids);
+        for (XmlUser user : contents.getUsers()) {
+            if (idsSet.contains(mapToExternalId(user.getId()))) {
+                users.add(mapDirectoryUser(user));
+            }
+        }
+        return users;
+    }
+
+    @Override
+    public DirectoryGroup findGroup(String name) {
+        List<XmlGroup> groups = contents.getGroups();
+        for (XmlGroup group : groups) {
+            if (group.getName().equals(name)) {
+                return mapDirectoryGroup(group);
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public DirectoryGroup findGroup(ExternalId id) {
+        List<XmlGroup> groups = contents.getGroups();
+        for (XmlGroup group : groups) {
+            if (mapToExternalId(group.getId()).equals(id)) {
+                return mapDirectoryGroup(group);
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public List<DirectoryUser> queryUsers(String query) {
+        List<DirectoryUser> returnList = new ArrayList<DirectoryUser>();
+        for (XmlUser user : contents.getUsers()) {
+            returnList.add(mapDirectoryUser(user));
+        }
+        return returnList;
+    }
+
+    @Override
+    public List<DirectoryGroup> queryGroups(String query) {
+        List<DirectoryGroup> groupList = new ArrayList<DirectoryGroup>();
+        for (XmlGroup group : contents.getGroups()) {
+            groupList.add(mapDirectoryGroup(group));
+        }
+        return groupList;
+    }
+
+}
diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryContents.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryContents.java
new file mode 100644
index 0000000..d883d4a
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryContents.java
@@ -0,0 +1,43 @@
+package org.ovirt.engine.core.authentication.xml;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementWrapper;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "Directory")
+public class XmlDirectoryContents {
+
+    @XmlElementWrapper(name = "Users")
+    @XmlElement(name = "User")
+    private List<XmlUser> users;
+
+    @XmlElementWrapper(name = "Groups")
+    @XmlElement(name = "group")
+    private List<XmlGroup> groups;
+
+    public List<XmlUser> getUsers() {
+        if (users == null) {
+            users = new ArrayList<>();
+        }
+        return users;
+    }
+
+    public void setUsers(List<XmlUser> users) {
+        this.users = users;
+    }
+
+    public List<XmlGroup> getGroups() {
+        if (groups == null) {
+            groups = new ArrayList<>();
+        }
+        return groups;
+    }
+
+    public void setGroups(List<XmlGroup> groups) {
+        this.groups = groups;
+    }
+
+}
diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryFactory.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryFactory.java
new file mode 100644
index 0000000..4921172
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlDirectoryFactory.java
@@ -0,0 +1,49 @@
+package org.ovirt.engine.core.authentication.xml;
+
+import java.io.File;
+
+import org.ovirt.engine.core.authentication.Configuration;
+import org.ovirt.engine.core.authentication.Directory;
+import org.ovirt.engine.core.authentication.DirectoryFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class XmlDirectoryFactory implements DirectoryFactory {
+
+    private static final Logger log = 
LoggerFactory.getLogger(XmlDirectoryFactory.class);
+
+    private static final String NAME_PARAMETER = "name";
+    private static final String XML_FILE_NAME_PARAMETER = "file";
+
+    private static final String type = "xml";
+    @Override
+    public String getType() {
+        return type;
+    }
+
+    @Override
+    public Directory create(File file, Configuration config) {
+        // Get the name of the authenticator:
+        String name = config.getInheritedString(NAME_PARAMETER);
+        if (name == null) {
+            log.error(
+                    "The configuration file \"{}\" doesn't contain the name of 
the directory.",
+                    file.getAbsolutePath()
+                    );
+            return null;
+        }
+
+        // Get the xml file that contains password info:
+        String xmlFileName = 
config.getInheritedString(XML_FILE_NAME_PARAMETER);
+        if (name == null) {
+            log.error(
+                    "The configuration file \"{}\" doesn't contain the xml 
file name.",
+                    file.getAbsolutePath()
+                    );
+            return null;
+        }
+
+        return new XmlDirectory(name, xmlFileName);
+    }
+
+}
diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlGroup.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlGroup.java
new file mode 100644
index 0000000..a9ac919
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlGroup.java
@@ -0,0 +1,23 @@
+package org.ovirt.engine.core.authentication.xml;
+
+public class XmlGroup {
+    private String Id;
+
+    public String getId() {
+        return Id;
+    }
+
+    public void setId(String id) {
+        Id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+    private String name;
+
+}
diff --git 
a/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlUser.java
 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlUser.java
new file mode 100644
index 0000000..898144f
--- /dev/null
+++ 
b/backend/manager/modules/authentication/src/main/java/org/ovirt/engine/core/authentication/xml/XmlUser.java
@@ -0,0 +1,95 @@
+package org.ovirt.engine.core.authentication.xml;
+
+public class XmlUser {
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getLastName() {
+        return lastName;
+    }
+
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getDepartment() {
+        return department;
+    }
+
+    public void setDepartment(String department) {
+        this.department = department;
+    }
+
+    public String getId() {
+        return Id;
+    }
+
+    public void setId(String id) {
+        Id = id;
+    }
+
+    public String getMemberOf() {
+        return memberOf;
+    }
+
+    public void setMemberOf(String memberOf) {
+        this.memberOf = memberOf;
+    }
+
+    public boolean isAdmin() {
+        return admin;
+    }
+
+    public void setAdmin(boolean admin) {
+        this.admin = admin;
+    }
+
+    public String getEmail() {
+        return email;
+    }
+
+    public void setEmail(String email) {
+        this.email = email;
+    }
+
+    private String userName;
+    private String name;
+    private String lastName;
+    private String password;
+    private String title;
+    private String department;
+    private String email;
+    private String Id;
+    private String memberOf;
+    private boolean admin;
+
+}


-- 
To view, visit http://gerrit.ovirt.org/22360
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I379ec6cffc10a8fb456eb3a52666665bf2331e42
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Yair Zaslavsky <yzasl...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to